diff --git a/AmazingCard/AmazingCard.xcodeproj/project.pbxproj b/AmazingCard/AmazingCard.xcodeproj/project.pbxproj index 4f54996..84c8d3f 100644 --- a/AmazingCard/AmazingCard.xcodeproj/project.pbxproj +++ b/AmazingCard/AmazingCard.xcodeproj/project.pbxproj @@ -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 */; }; @@ -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 = ""; }; + 3E1238B621BFCF1F00E095A3 /* ACEditViewController+Brush.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "ACEditViewController+Brush.m"; sourceTree = ""; }; + 3E1238B821BFCF5500E095A3 /* ACEditViewController+Circle.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ACEditViewController+Circle.h"; sourceTree = ""; }; + 3E1238B921BFCF5500E095A3 /* ACEditViewController+Circle.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "ACEditViewController+Circle.m"; sourceTree = ""; }; 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 = ""; }; 3E2CC1E221B814F900273EC3 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; @@ -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; @@ -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 */, diff --git a/AmazingCard/AmazingCard.xcworkspace/xcuserdata/yangqi.xcuserdatad/UserInterfaceState.xcuserstate b/AmazingCard/AmazingCard.xcworkspace/xcuserdata/yangqi.xcuserdatad/UserInterfaceState.xcuserstate index b64cfa2..78f661c 100644 Binary files a/AmazingCard/AmazingCard.xcworkspace/xcuserdata/yangqi.xcuserdatad/UserInterfaceState.xcuserstate and b/AmazingCard/AmazingCard.xcworkspace/xcuserdata/yangqi.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/AmazingCard/AmazingCard/Edit/ACEditViewController+Brush.h b/AmazingCard/AmazingCard/Edit/ACEditViewController+Brush.h new file mode 100644 index 0000000..db9ea4c --- /dev/null +++ b/AmazingCard/AmazingCard/Edit/ACEditViewController+Brush.h @@ -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 diff --git a/AmazingCard/AmazingCard/Edit/ACEditViewController+Brush.m b/AmazingCard/AmazingCard/Edit/ACEditViewController+Brush.m new file mode 100644 index 0000000..979e237 --- /dev/null +++ b/AmazingCard/AmazingCard/Edit/ACEditViewController+Brush.m @@ -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 diff --git a/AmazingCard/AmazingCard/Edit/ACEditViewController+Circle.h b/AmazingCard/AmazingCard/Edit/ACEditViewController+Circle.h new file mode 100644 index 0000000..493bf89 --- /dev/null +++ b/AmazingCard/AmazingCard/Edit/ACEditViewController+Circle.h @@ -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 diff --git a/AmazingCard/AmazingCard/Edit/ACEditViewController+Circle.m b/AmazingCard/AmazingCard/Edit/ACEditViewController+Circle.m new file mode 100644 index 0000000..b3e752c --- /dev/null +++ b/AmazingCard/AmazingCard/Edit/ACEditViewController+Circle.m @@ -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 diff --git a/AmazingCard/AmazingCard/Edit/ACEditViewController.h b/AmazingCard/AmazingCard/Edit/ACEditViewController.h index 1d77457..f409525 100644 --- a/AmazingCard/AmazingCard/Edit/ACEditViewController.h +++ b/AmazingCard/AmazingCard/Edit/ACEditViewController.h @@ -8,6 +8,13 @@ #import +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 diff --git a/AmazingCard/AmazingCard/Edit/ACEditViewController.m b/AmazingCard/AmazingCard/Edit/ACEditViewController.m index 3e34a9a..9d1685a 100644 --- a/AmazingCard/AmazingCard/Edit/ACEditViewController.m +++ b/AmazingCard/AmazingCard/Edit/ACEditViewController.m @@ -7,15 +7,13 @@ // #import "ACEditViewController.h" +#import "ACEditViewController+Circle.h" +#import "ACEditViewController+Brush.h" -static CGFloat offset = 20; @interface ACEditViewController () -@property (nonatomic, strong) UIImageView *imageView; -@property (nonatomic, strong) UIView *clipView; -@property (nonatomic, assign) CGPoint startP; -@property (nonatomic, assign) CGPoint curP; + @end @@ -25,127 +23,9 @@ - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; - _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"amazingCard"]]; - _imageView.contentMode = UIViewContentModeScaleAspectFit; - _imageView.userInteractionEnabled = YES; - [self.view addSubview:_imageView]; - - _imageView.frame = self.view.bounds; - - UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; - [_imageView addGestureRecognizer:pan]; - - UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; - [_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:)]; + [self circle]; } -- (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; - [pan setTranslation:CGPointMake(0, 0) inView:pan.view]; - return; - } - -// if (CGRectContainsPoint(self.clipView.frame, p)) { -// return; -// } else { -// CGFloat x = p.x; -// CGFloat y = p.y; -// } - 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 { - 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