ios-UIGesture扩展手势

1、平滑手势

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"01"]];
    imageView.frame=CGRectMake(0, 0, 200, 200);
    imageView.userInteractionEnabled=YES;//设置图像可交互
    //创建一个平滑手势
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panClick:)];
    [imageView addGestureRecognizer:pan];//将手势添加到图像视图中
    [self.view addSubview:imageView];
}
//只有手指按住屏幕在屏幕上运动的时候就会执行这个函数也就是说只要手指在屏幕上发生任何坐标变化都会调用该函数
-(void)panClick:(UIPanGestureRecognizer *)pan
{
    //获取移动的坐标相对于该视图对象
    CGPoint pt=[pan translationInView:self.view];
    NSLog(@"pt.x=%.2f, pt.y=%.2f",pt.x,pt.y);
    //获取移动时的相对速度
    CGPoint pv=[pan velocityInView:self.view];
}
在imageView中取消手势

[imageView removeGestureRecognizer:pan];

2、滑动手势

UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];
    //设置滑动手势接受事件的类型
    swipe.direction=UISwipeGestureRecognizerDirectionLeft;
    //UISwipeGestureRecognizerDirectionLeft:向左滑动
    //UISwipeGestureRecognizerDirectionRight:向右滑动
    //UISwipeGestureRecognizerDirectionup:向上滑动
    //UISwipeGestureRecognizerDirectionDown:向下滑动
//我们这里也可以设置两个滑动手势 

swipe.direction=UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
//如果我们只想添加一个点击函数的话,我们就要在这个点击函数中进行确定是滑向那一边的手势
3、长按手势

 UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(pressLong:)];
    [imageView addGestureRecognizer:longPress];
    //设置长按手势的时间,默认0.5s为长按手势,小于0.5秒的就为点击手势了
    longPress.minimumPressDuration=0.5;

-(void)pressLong:(UILongPressGestureRecognizer *)press
{
    //手指的状态对象,当长按达到0.5s的时候就会触发函数
    if(press.state==UIGestureRecognizerStateBegan)
    {
        NSLog(@"状态开始");
    }
    //当手指离开屏幕时就是结束状态
    else if(press.state==UIGestureRecognizerStateEnded)
    {
        NSLog(@"结束状态");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值