#import "MainViewController.h"
@interface MainViewController ()
@property(nonatomic, retain)UIImageView *imageView;
@property(nonatomic, retain)UIAlertView *alertView;
@end
@implementation MainViewController
- (void)dealloc
{
[_imageView release];
[_alertView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// UIImageView
UIImage *image = [UIImage imageNamed:@"22.jpg"];
self.imageView = [[UIImageView alloc]initWithImage:image];
self.imageView.frame = CGRectMake(80, 100,200, 300) ;
[self.view addSubview:self.imageView];
[_imageView release];
// 把图片的用户交互打开,它默认是关闭的,此外还有一个空间是label
self.imageView.userInteractionEnabled = YES;
//手势的作用
// 1.点击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
// 2.设置点击几次才会触发方法
tap.numberOfTapsRequired = 2;
// 3.设置几根手指进行点击
tap.numberOfTouchesRequired = 2;
// 4.将手势添加到对应的图片上
[self.imageView addGestureRecognizer:tap];
[tap release];
// 2.长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
// 设置长按触发的最小时间
longPress.minimumPressDuration = 2;
// 允许用户手指在长按过程中允许移动的距离
longPress.allowableMovement = 200;
// 把手势添加到图片上
[self.imageView addGestureRecognizer:longPress];
[longPress release];
// 3.旋转
// 创建一旋转的手势
UIRotationGestureRecognizer *rotaTion = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
// 把手势放到对应的图片上
[self.imageView addGestureRecognizer:rotaTion];
// 释放
[rotaTion release];
// 4.捏合
// 创建
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
// 放到图片上
[self.imageView addGestureRecognizer:pinch];
// 创建
[pinch release];
// 5.拖拽
// 创建
UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureAction:)];
// 添加手势
[self.imageView addGestureRecognizer:panGesture];
// 释放
[panGesture release];
// 6.轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
[self.imageView addGestureRecognizer:swipe];
[swipe release];
// 轻扫的方向
//向右
swipe.direction = UISwipeGestureRecognizerDirectionRight ;
// 7.屏幕边际手势,iOS7.0之后的手势
//UIScreenEdgePanGestureRecognizer
}
#pragma mark 点击的方法
- (void)tapAction:(UITapGestureRecognizer *)tap{
NSLog(@"测试一下点击手势");
self.imageView.image = [UIImage imageNamed:@"33.jpg"];
}
#pragma mark 长按对应的响应方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress{
/*长按的状态
longPress.state*/
//长安后弹出一个UIAlertView
if (!self.alertView) {
self.alertView = [[UIAlertView alloc]initWithTitle:nil message:@"跳转" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[self.alertView show];
[_alertView release];
self.alertView = nil;
}
}
#pragma mark 通过图片的旋转手势,让图片发生旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
// 可以通过手势获取手势添加的视图是哪一个
UIImageView *imageView = (UIImageView *)rotation.view;
// 进行旋转的操作
//通过视图的transform属性,让视图进行旋转
// imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
#pragma mark 捏合, 缩放图片
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
// 通过手势找到对应的视图
UIImageView *imageView = (UIImageView *)pinch.view;
// 通过transform改变图片尺寸
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
// 为了防止手势的变化让图片直接消失
pinch.scale = 1;
}
#pragma mark 通过拖拽的手势,让视图随着手势的移动而移动
- (void)panGestureAction:(UIPanGestureRecognizer *)panGest{
// 通过手势找视图
UIImageView *imageView = (UIImageView *)panGest.view;
// 通过手势获得经过的点
CGPoint p = [panGest translationInView: imageView];
// 设置移动的位置
imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
// 为了防止手势在操作的时候在视图消失
[panGest setTranslation:CGPointZero inView:imageView];
}
#pragma mark 轻扫
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
@end
UI_手势
最新推荐文章于 2024-04-14 10:12:22 发布