UIGesture扩展手势:UIPan平移、UISwipe滑动、

2017-10-13  本文已影响0人  李琪_59dc

重点属性

UIPanGestureRecognizer平移手势

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *iView = [[UIImageView alloc]initWithImage:[UIImage           imageNamed:@"icon1"]];
    iView.frame = CGRectMake(50, 50, 200, 300);
    iView.userInteractionEnabled = YES;
    [self.view addSubview:iView];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer   alloc]initWithTarget:self action:@selector(panAct:)];
    [iView addGestureRecognizer:pan];
}

//移动事件函数,只要手指坐标在屏幕上发生变化时,就调用
-(void)panAct:(UIPanGestureRecognizer*)pan{
    //获取移动的坐标,相对于视图的坐标系
    //参数:相对的视图对象
    CGPoint pt = [pan translationInView:self.view];
    NSLog(@"pan!!");
    NSLog(@"pt.x = %.2f, pt.y = %.2f",pt.x,pt.y);
    
    CGPoint pv = [pan velocityInView:self.view];
    NSLog(@"pt.x = %.2f, pt.y = %.2f",pv.x,pv.y);
}

UISwipeGestureRecognizer滑动手势

- (void)viewDidLoad {
    [super viewDidLoad];
//创建一个滑动手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAct:)];
    //设定滑动手势接受事件的类型
    //UISwipeGestureRecognizerDirectionLeft向左滑动
    //UISwipeGestureRecognizerDirectionRight
    //UISwipeGestureRecognizerDirectionUp
    //UISwipeGestureRecognizerDirectionDown
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    //不能写一起 swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
    //要分开创建,然后遵守手势协议,实现“是否支持多个手势”
    [iView addGestureRecognizer:swipe];
}
-(void)swipeAct:(UISwipeGestureRecognizer*)swipe{
    NSLog(@"向左滑动");
}

UILongPressGestureRecognizer长按手势

- (void)viewDidLoad {
    [super viewDidLoad];

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(pressLong:)];
    [iView addGestureRecognizer:longPress];
    
    //设置长按手势的时间,默认值0.5s
    longPress.minimumPressDuration = 0.5;
}

-(void)pressLong:(UILongPressGestureRecognizer*)press{
    //到达时间调用一次,手指离开又调用一次
    NSLog(@"长按手势llllllll");
    if(press.state == UIGestureRecognizerStateBegan){
        NSLog(@"状态开始");
    }
    else if(press.state == UIGestureRecognizerStateEnded){
        NSLog(@"状态结束");
    }
}
上一篇下一篇

猜你喜欢

热点阅读