UI基础 手势(上)

root m

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imageV=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 500)];
    imageV.image =[UIImage imageNamed:@"a.jpg"];
    [self.view addSubview:imageV];
   // 打开用户交互事件
    
    imageV.userInteractionEnabled=YES;
    //六大手势
    //1.点击手势
    //创建手势
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMe)];
    
    //控制点击次数
    tap.numberOfTapsRequired=3;
    
    //把手势添加到图片上
    [imageV addGestureRecognizer:tap];
    
    //2.长按手势
    UILongPressGestureRecognizer *longP=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPresssMe:)];
    //控制长按事件 (按多少秒触发)
    longP.minimumPressDuration=3;
    //控制长按过程中手指允许移动多少
    longP.allowableMovement=50;
    [imageV addGestureRecognizer:longP];
    
    
    //3.轻扫手势
    UISwipeGestureRecognizer* swip=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipMe:)];
    
    //指定方向
    swip.direction=UISwipeGestureRecognizerDirectionLeft;
    
    UISwipeGestureRecognizer* swip1=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipMe1:)];
    
    //指定方向
    swip.direction=UISwipeGestureRecognizerDirectionLeft;
    [imageV addGestureRecognizer:swip];

    
}

//轻扫手势对应的方法
-(void)swipMe:(UISwipeGestureRecognizer *)swipe
{
    if(swipe.direction=UISwipeGestureRecognizerDirectionLeft)
    {
        NSLog(@"向左扫");
    }else if (swipe.direction==UISwipeGestureRecognizerDirectionDown)
    {
        NSLog(@"向下扫");
    }
}





//长按手势对应方法
//想要实现什么功能 就可以写在哪个模块里
-(void)longPresssMe:(UILongPressGestureRecognizer *)longPre
{
    
    if(longPre.state==UIGestureRecognizerStateBegan){
        NSLog(@"开始");
    }else if (longPre.state==UIGestureRecognizerStateChanged){
        NSLog(@"手抖");
    }else if (longPre.state= UIGestureRecognizerStateEnded)
    {
        NSLog(@"结束");
    }
}

//点击手势对应的方法
-(void)tapMe
{
    NSLog(@"已打印");
    
}

@end

猜你喜欢

转载自www.cnblogs.com/zhangqing979797/p/13394309.html