手勢,有規(guī)律的觸摸事件的封裝
手勢類,抽象類,使用他的子類
UIGestureRecognizer 手勢識別器
輕拍
UITapGestureRecognizer
長按
UILongPressGestureRecognizer
旋轉
UIRotationGestureRecognizer
捏合
UIPinchGestureRecognizer
平移
UIPanGestureRecognizer
清掃
UISwipeGestureRecognizer
屏幕邊緣清掃
UIScreenEdgePanGestureRecognizer
self.view.backgroundColor = [UIColor lightGrayColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, 350, 350)];
imageView.backgroundColor = [UIColor greenColor];
[self.view addSubview:imageView];
imageView.image = [UIImage imageNamed:@"6.jpg"];
imageView.contentMode = UIViewContentModeCenter;
//imageView, label 的用戶交互默認是關閉的
imageView.userInteractionEnabled = YES;
//輕拍
//Target: 由誰來調用這個方法
//? 多態(tài) =>
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//需要輕拍幾次
tap.numberOfTapsRequired = 1;
//需要幾根手指
tap.numberOfTouchesRequired = 1;
//將輕拍手勢添加到imageView上,別的控件也可以添加手勢
[imageView addGestureRecognizer:tap];
}
//清掃
//在平移方法實現(xiàn)的情況下,清掃的優(yōu)先級就沒有平移的高
// {
// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// //從左往右劃,沖右往左劃
// //對于NS_OPTIONS類型的枚舉,可以使用或( | )來連接多個值
// //不能同時支持上下左右滑動
// swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
// swipe.numberOfTouchesRequired = 1;
// [imageView addGestureRecognizer:swipe];
// }
//長按
// {
// UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
//
// //長按運行了兩次
// [imageView addGestureRecognizer:longPress];
// }
//旋轉
// {
// UIRotationGestureRecognizer *rotain = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
//
// [imageView addGestureRecognizer:rotain];
// }
//平移
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
}
//捏合
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
}
}
//方法寫在那里就是哪里的方法
-
(void)tapAction:(UITapGestureRecognizer *)tap{
NSLog(@"點擊");
//tap.view 獲取手勢所在的視圖
//強轉保證要強轉的類型就是你需要的類型
UIImageView *imageView = (UIImageView *)tap.view;
imageView.image = [UIImage imageNamed:@"3.jpg"];
//怎么多次改變
}
-
(void)swipeAction:(UISwipeGestureRecognizer *)swipe{
NSLog(@"清掃");
UIImageView *imageView = (UIImageView *)swipe.view;
imageView.image = [UIImage imageNamed:@"3.jpg"];
} -
(void)longAction:(UILongPressGestureRecognizer *)longPress{
//判斷,在長按手勢生效的時候執(zhí)行一次代碼
if (longPress.state == UIGestureRecognizerStateBegan) {NSLog(@"長按"); UIImageView *imageView = (UIImageView *)longPress.view; imageView.image = [UIImage imageNamed:@"3.jpg"];}
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
// 動畫, transform2D放射變換
UIImageView *imageView = (UIImageView *)rotation.view;
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
-
(void)panAction:(UIPanGestureRecognizer *)pan{
UIImageView *imageView = (UIImageView *)pan.view;
//獲取平移手勢在視圖上移動的距離
CGPoint change = [pan translationInView:imageView];
imageView.transform = CGAffineTransformTranslate(imageView.transform, change.x , change.y);
[pan setTranslation:CGPointZero inView:imageView];
}
-
(void)pinchAction:(UIPinchGestureRecognizer *)pinch{
UIImageView *imageView = (UIImageView *)pinch.view;
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
//復位
pinch.scale = 1;
}