手势

来源:互联网 发布:linux bash 逻辑表达式 编辑:程序博客网 时间:2024/05/01 06:44
#import "RootViewController.h"@interface RootViewController ()@property(nonatomic, retain)UIImageView *imageView;@end@implementation RootViewController- (void)dealloc{    [_imageView release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor = [UIColor whiteColor];    // 创建一个imageView    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"....."]];    self.imageView.frame = CGRectMake(100, 200, 200, 200);    [self.view addSubview:self.imageView];    [_imageView release];    NSLog(@"%@", self.imageView);    // 图片如果要添加手势,要把图片的用户交互打开,它和label默认是NO    self.imageView.userInteractionEnabled = YES;    NSLog(@"%@", self.imageView);    // 控件的交互如果打开,打印控件对象没有userInteractionEnabled = NO这句话,就可以根据有没有这句话判断交互有没有打开   // 1.点击   UITapGestureRecognizer *tap = [[UITapGesyureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];   // 把手势加到指定的视图上   [self.imageView addGestureRecognizer:tap];   // 需要点击两次才反应   tap.numberOfTapsRequired = 2;   // 需要两根手指同时按才能触发   tap.numberOfTouchesRequired = 2;   // 2.长按   UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];    // 把手势加到上imageView上   [self.imageView addGestureRecognizer:longPress];   [longPress release];   // 设置长按触发时间   longPress.minimumPressDuration = 1;   // 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 *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction)];   [self.imageView addGestureRecognizer:pan];   [pan release];   // 6.轻扫   UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];   [self.imageView addGestureRecognizer:swipe];   [swipe release];   // 轻扫的方向   swipe.direction = UISwipeGestureRecognizerDirectionLeft;  // 7.屏幕边界手势 UIScreenEdgePanGestureRecognizer  UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanAction:)];  [self.imageView addGestureRecognizer:screenEdgePan];  [screenEdgePan release];}// 轻扫- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {   if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {   NSLog(@"向左进行轻扫");   }  }// 拖拽的手势手法- (void)panAction:(UIPanGestureRecognizer *)pan {    // 通过手势找视图    UIImageView *imageView = (UIImageView *)pan.view;    // 通过手势来获取移动的点    CGPoint p = [pan translationInView:imageView];    // 通过设置transform实现拖拽    imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);    [pan setTranslation:CGPointZero inView:imageView];   }// 捏合的手势方法- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {    // 通过手势找视图    UIImageView *imageView = (UIImageView *)pinch.view;    // 让视图进行缩放    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);    NSLog(@"%g", pinch.,scale);    pinch.scale = 0;  }// 旋转的方法- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {  // 可以通过手势,知道手势添加到哪个视图上  UIImageView *imageView = (UIImageView *)rotation.view;  // 找到视图,并且让视图进行旋转  imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);  rotation.rotation = 1; }// 长按的方法- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {    if (longPress.state == UIGestureRecognizerStateBegan) {      NSLog(@"长按开始");    }   }// 点击之后触发的方法- (void)tapAction:(UITapGestureRecognizer *)tap {    NSLog(@"我被点击了");   }
0 0