#import "HYCImageCropper.h"
@interface HYCImageCropper ()
{
UIImageView *imageView;
}
//rotation gesture需要的中間變量,記錄上一個rotation參數
@property CGFloat lastRotation;
@end
@implementation HYCImageCropper
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(id)initWithFrame:(CGRect)frame Image:(UIImage *)image Area:(CGRect)area
{
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor whiteColor];
//add image view and its image
imageView = [[UIImageView alloc]initWithFrame:self.bounds];
[imageView setImage:image];
[self addSubview: imageView];
[imageView setUserInteractionEnabled:YES];
//add gestures on imageView
UIRotationGestureRecognizer *rotateGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImage:)];
rotateGes.delegate = self;
[imageView addGestureRecognizer:rotateGes];
UIPinchGestureRecognizer *scaleGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleImage:)];
scaleGes.delegate = self;
[imageView addGestureRecognizer:scaleGes];
UIPanGestureRecognizer *moveGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)];
moveGes.delegate = self;
[imageView addGestureRecognizer:moveGes];
return self;
}
- (void)moveImage:(UIPanGestureRecognizer *)sender
{
NSLog(@"move!");
//使目標view升到表層
[self bringSubviewToFront:imageView];
//求當前gesture在主view中的座標
CGPoint location = [sender locationInView:self];
//利用center屬性直接定義目標view位置
sender.view.center = CGPointMake(location.x, location.y);
}
- (void)scaleImage:(UIPinchGestureRecognizer *)sender
{
NSLog(@"scale!");
//使目標view升到表層
[self bringSubviewToFront:sender.view];
//求當前gesture在主view中的座標
CGPoint location = [sender locationInView:self];
sender.view.center = CGPointMake(location.x, location.y);
//用core graph裏面封裝好的CGAffineTransformScale做scale變換
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
sender.scale = 1;
}
- (void)rotateImage:(UIRotationGestureRecognizer *)sender
{
NSLog(@"rotate");
//使目標view升到表層
[self bringSubviewToFront:sender.view];
//求當前gesture在主view中的座標
CGPoint location = [sender locationInView:self];
sender.view.center = CGPointMake(location.x, location.y);
//如果沒有旋轉,就把lastRotate參數置爲0
if ([sender state] == UIGestureRecognizerStateEnded) {
self.lastRotation = 0;
return;
}
//找到上一次的transform參數,以便接着旋轉
CGAffineTransform currentTransform = imageView.transform;
//求出新的旋轉參數,並使用CGAffineTransformRotate
CGFloat rotation = 0.0 - (self.lastRotation - sender.rotation);
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotation);
imageView.transform = newTransform;
//當rotate gesture停了,就記錄這時候的狀態到lastRotate這個變量,一邊下一次接着旋轉
self.lastRotation = sender.rotation;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
@end
iOS的手勢控制
今天做了一下iOS的手勢控制
手勢自然就是在真機上面,除了點擊這種短暫動作之外的操作,主要包括:
1 Pan
Pan就是最常見的拖動了。單個手指從屏幕滑動,系統會識別出這是一個pan gesture,當然代碼裏面需要包含一些固有的定義,纔可以對這個手勢做反應。
注:在xcode的模擬器上面,只需要按住鼠標(or觸摸板)不放,然後拖動,就是模仿真機中的pan了。
2 Rotation
當兩個手指同時觸摸在屏幕,然後做同旋轉方向的曲線拖動,就是Rotation 這個gesture
注:在xcode的模擬器上面,需要按住鍵盤的Option,同時按住鼠標(or觸摸板)並開始拖動,模擬器上會模擬出兩個小白圓,作爲rotation。
3 Pinch
當兩個手指同時觸摸在屏幕,然後做反向直線拖動,就是Pinch
注:在xcode的模擬器上面,需要按住鍵盤的Option,同時按住鼠標(or觸摸板)並開始拖動,模擬器上會模擬出兩個小白圓,作爲rotation。
前端時間做了一個剪切圖片的cropper,其中用到了手勢控制,直接整個.m分享出來,應該能看懂的。。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.