Skip to content

Commit

Permalink
修改悬浮选择框的手势
Browse files Browse the repository at this point in the history
  • Loading branch information
YangQi committed Dec 12, 2018
1 parent 79ebd18 commit c6b540d
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 124 deletions.
12 changes: 12 additions & 0 deletions AmazingCard/AmazingCard.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
3E1238B721BFCF1F00E095A3 /* ACEditViewController+Brush.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E1238B621BFCF1F00E095A3 /* ACEditViewController+Brush.m */; };
3E1238BA21BFCF5500E095A3 /* ACEditViewController+Circle.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E1238B921BFCF5500E095A3 /* ACEditViewController+Circle.m */; };
3E2CC1E321B814F900273EC3 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E2CC1E221B814F900273EC3 /* AppDelegate.m */; };
3E2CC1EB21B814F900273EC3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3E2CC1EA21B814F900273EC3 /* Assets.xcassets */; };
3E2CC1F121B814F900273EC3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E2CC1F021B814F900273EC3 /* main.m */; };
Expand Down Expand Up @@ -41,6 +43,10 @@

/* Begin PBXFileReference section */
19B80172F905FA030FCBA7B3 /* libPods-AmazingCard.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-AmazingCard.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3E1238B521BFCF1F00E095A3 /* ACEditViewController+Brush.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ACEditViewController+Brush.h"; sourceTree = "<group>"; };
3E1238B621BFCF1F00E095A3 /* ACEditViewController+Brush.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "ACEditViewController+Brush.m"; sourceTree = "<group>"; };
3E1238B821BFCF5500E095A3 /* ACEditViewController+Circle.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ACEditViewController+Circle.h"; sourceTree = "<group>"; };
3E1238B921BFCF5500E095A3 /* ACEditViewController+Circle.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "ACEditViewController+Circle.m"; sourceTree = "<group>"; };
3E2CC1DE21B814F900273EC3 /* AmazingCard.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AmazingCard.app; sourceTree = BUILT_PRODUCTS_DIR; };
3E2CC1E121B814F900273EC3 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
3E2CC1E221B814F900273EC3 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -227,6 +233,10 @@
children = (
3EA58EEA21B824D8006B4951 /* ACEditViewController.h */,
3EA58EEB21B824D8006B4951 /* ACEditViewController.m */,
3E1238B821BFCF5500E095A3 /* ACEditViewController+Circle.h */,
3E1238B921BFCF5500E095A3 /* ACEditViewController+Circle.m */,
3E1238B521BFCF1F00E095A3 /* ACEditViewController+Brush.h */,
3E1238B621BFCF1F00E095A3 /* ACEditViewController+Brush.m */,
3EA1403721BEAE3C0040B488 /* amazingCard.png */,
);
path = Edit;
Expand Down Expand Up @@ -421,8 +431,10 @@
3E2CC22021B815A700273EC3 /* BaseTableView.m in Sources */,
3E2CC1F121B814F900273EC3 /* main.m in Sources */,
3EA58EEC21B824D8006B4951 /* ACEditViewController.m in Sources */,
3E1238B721BFCF1F00E095A3 /* ACEditViewController+Brush.m in Sources */,
3E2CC21F21B815A700273EC3 /* BaseViewController.m in Sources */,
3E2CC1E321B814F900273EC3 /* AppDelegate.m in Sources */,
3E1238BA21BFCF5500E095A3 /* ACEditViewController+Circle.m in Sources */,
3E2CC22121B815A700273EC3 /* ACHomeVCTableViewCell.m in Sources */,
3E2CC22B21B8196300273EC3 /* ACDevice.m in Sources */,
3E2CC22221B815A700273EC3 /* ACHomeViewController.m in Sources */,
Expand Down
Binary file not shown.
15 changes: 15 additions & 0 deletions AmazingCard/AmazingCard/Edit/ACEditViewController+Brush.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// ACEditViewController+Brush.h
// AmazingCard
//
// Created by 杨琦 on 2018/12/11.
// Copyright © 2018 A.C. All rights reserved.
//

#import "ACEditViewController.h"

@interface ACEditViewController (Brush)

- (void)brush;

@end
17 changes: 17 additions & 0 deletions AmazingCard/AmazingCard/Edit/ACEditViewController+Brush.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ACEditViewController+Brush.m
// AmazingCard
//
// Created by 杨琦 on 2018/12/11.
// Copyright © 2018 A.C. All rights reserved.
//

#import "ACEditViewController+Brush.h"

@implementation ACEditViewController (Brush)

- (void)brush {

}

@end
15 changes: 15 additions & 0 deletions AmazingCard/AmazingCard/Edit/ACEditViewController+Circle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// ACEditViewController+Circle.h
// AmazingCard
//
// Created by 杨琦 on 2018/12/11.
// Copyright © 2018 A.C. All rights reserved.
//

#import "ACEditViewController.h"

@interface ACEditViewController (Circle)

- (void)circle;

@end
159 changes: 159 additions & 0 deletions AmazingCard/AmazingCard/Edit/ACEditViewController+Circle.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
//
// ACEditViewController+Circle.m
// AmazingCard
//
// Created by 杨琦 on 2018/12/11.
// Copyright © 2018 A.C. All rights reserved.
//

#import "ACEditViewController+Circle.h"

@implementation ACEditViewController (Circle)

- (void)circle {

self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"amazingCard"]];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.userInteractionEnabled = YES;
[self.view addSubview:self.imageView];

self.imageView.frame = self.view.bounds;

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.imageView addGestureRecognizer:tap];

UIView *clipV = [[UIView alloc] init];
clipV.backgroundColor = [UIColor redColor];
clipV.alpha = 0.5;
[self.view addSubview:clipV];
self.clipView = clipV;
self.clipView.hidden = YES;
self.clipView.frame = CGRectMake(self.view.center.x, self.view.center.y, 25, 25);
UIPanGestureRecognizer *clipViewPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.clipView addGestureRecognizer:clipViewPan];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(barButtonItem:)];
}

- (void)tap:(UIGestureRecognizer *)tap {
if (CGPointEqualToPoint(self.startP, CGPointZero)) {
CGPoint p = [tap locationInView:self.imageView];
NSLog(@"x = %f, y = %f", p.x, p.y);
self.clipView.frame = CGRectMake(0, 0, offset*2, offset*2);
self.clipView.center = p;
self.clipView.hidden = NO;
self.startP = self.clipView.frame.origin;
}
}

- (void)pan:(UIPanGestureRecognizer *)pan {

CGPoint p = [pan locationInView:self.imageView];
NSLog(@"x = %f, y = %f", p.x, p.y);

//添加拖拽
if ([pan.view isEqual:self.clipView]) {
// if (self.clipView.frame.size.width > offset*2
// && self.clipView.frame.size.height > offset*2)
// {
// //超出范围不可拖拽
// CGRect frame = CGRectMake(self.clipView.frame.origin.x + offset/2, self.clipView.frame.origin.y+offset/2, self.clipView.frame.size.width-offset, self.clipView.frame.size.height-offset);
// if (!CGRectContainsPoint(frame, p)) {
// return;
// }
// }

CGPoint translation = [pan translationInView:pan.view.superview];
CGPoint finalCenter = CGPointMake(pan.view.center.x + translation.x, pan.view.center.y + translation.y);
pan.view.center = finalCenter;
self.startP = self.clipView.frame.origin;
self.curP = CGPointMake(self.startP.x + self.clipView.frame.size.width, self.startP.y + self.clipView.frame.size.height);
[pan setTranslation:CGPointMake(0, 0) inView:pan.view];
return;
}

//startP是否为空(第一次)
if (CGPointEqualToPoint(self.startP, CGPointZero)) {
self.startP = p;
self.startP = CGPointMake(p.x-offset, p.y-offset);
self.clipView.frame = CGRectMake(self.startP.x, self.startP.y, offset*2, offset*2);

} else {
if (p.x < self.clipView.center.x) {
if (p.y > self.clipView.center.y) {
self.startP = CGPointMake(p.x, self.startP.y);
self.curP = CGPointMake(self.curP.x, p.y);
CGFloat offsetX = self.curP.x - self.startP.x;
CGFloat offsetY = self.curP.y - self.startP.y;
self.clipView.frame = CGRectMake(self.startP.x, self.startP.y, offsetX, offsetY);
} else {
self.startP = p;
CGFloat offsetX = self.curP.x - self.startP.x;
CGFloat offsetY = self.curP.y - self.startP.y;
self.clipView.frame = CGRectMake(self.startP.x, self.startP.y, offsetX, offsetY);
}
} else {
if (p.y < self.clipView.center.y) {
self.startP = CGPointMake(self.startP.x, p.y);
self.curP = CGPointMake(p.x, self.curP.y);
CGFloat offsetX = self.curP.x - self.startP.x;
CGFloat offsetY = self.curP.y - self.startP.y;
self.clipView.frame = CGRectMake(self.startP.x, self.startP.y, offsetX, offsetY);
} else {
self.curP = p;
CGFloat offsetX = self.curP.x - self.startP.x;
CGFloat offsetY = self.curP.y - self.startP.y;
self.clipView.frame = CGRectMake(self.startP.x, self.startP.y, offsetX, offsetY);
}
}
}
self.clipView.hidden = NO;
}

- (void)barButtonItem:(UIBarButtonItem *)barButton {
CGRect clipViewframe = self.clipView.frame;
self.clipView.hidden = YES;
self.startP = CGPointZero;
self.curP = CGPointZero;

CGFloat imageCurWidth = self.view.bounds.size.width;
CGFloat imageCurHeight = imageCurWidth * self.imageView.image.size.height /self.imageView.image.size.width;

CGFloat curClipY = clipViewframe.origin.y - (self.view.bounds.size.height - imageCurHeight) / 2.0;
CGFloat curClipX = clipViewframe.origin.x;

CGSize size = self.imageView.image.size;
CGSize viewSize = self.view.frame.size;
CGFloat scale = viewSize.width / size.width * size.height < viewSize.height ? viewSize.width / size.width : viewSize.height / size.height;
CGFloat clipY = curClipY / scale;
CGFloat clipX = curClipX / scale;
CGFloat clipW = clipViewframe.size.width / scale;
CGFloat clipH = clipViewframe.size.height / scale;
CGRect clipFrame = CGRectMake(clipX, clipY, clipW, clipH);

UIImage *image = [self getImageFromImage:self.imageView.image rect:clipFrame];
self.imageView.image = image;
}

-(UIImage *)getImageFromImage:(UIImage *)image rect:(CGRect)rect {

//大图bigImage
//定义myImageRect,截图的区域
CGRect myImageRect = rect;
CGImageRef imageRef = image.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size = image.size;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();

return smallImage;

}

@end
7 changes: 7 additions & 0 deletions AmazingCard/AmazingCard/Edit/ACEditViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

#import <UIKit/UIKit.h>

static CGFloat offset = 20;

@interface ACEditViewController : UIViewController

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIView *clipView;
@property (nonatomic, assign) CGPoint startP;
@property (nonatomic, assign) CGPoint curP;

@end
Loading

0 comments on commit c6b540d

Please sign in to comment.