您的位置:首页 > 产品设计 > UI/UE

iOS开发从入门到精通--UIGesture手势扩展,平移,滑动,长按

2016-07-28 08:51 411 查看
UIGesture手势扩展,平移,滑动,长按





#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UIImageView * iView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"17_8.png"]];

iView.frame=CGRectMake(50, 50, 200, 300);

iView.userInteractionEnabled=YES;

//创建一个平移手势
//p1:事件函数处理对象
//p2:事件函数
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAct:)];

//将手势添加到图像视图中
[iView addGestureRecognizer:pan];

//将移动事件手势从图像视图中取消
[iView removeGestureRecognizer:pan];

[self.view addSubview:iView];

//创建一个滑动手势
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAct:)];

//设定滑动手势接收事件的类型
//UISwipeGestureRecognizerDirectionUp    上
//UISwipeGestureRecognizerDirectionDown  下
//UISwipeGestureRecognizerDirectionLeft  左
//UISwipeGestureRecognizerDirectionRight 右
swipe.direction =UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;

[iView addGestureRecognizer:swipe];

//创建长按手势
UILongPressGestureRecognizer * longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAct:)];
[iView addGestureRecognizer:longPress];

//设置长按手势的时间,默认是0.5秒的时间为长按手势
longPress.minimumPressDuration=3;

}
//长按函数
-(void)longPressAct:(UILongPressGestureRecognizer*)longpress{
//手势的状态对象,到达规定时间,3秒钟,触发函数
if(longpress.state==UIGestureRecognizerStateBegan){
NSLog(@"状态开始");
}
//当手指离开屏幕时,结束状态
else if(longpress.state==UIGestureRecognizerStateEnded){
NSLog(@"结束状态");

}

}

//滑动函数
-(void)swipeAct:(UISwipeGestureRecognizer*)swipe{
NSLog(@"swipe滑动");
}

//移动事件函数,只要手指坐标在屏幕上发生变化时,函数就会被调用
-(void)panAct:(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];
NSLog(@"pv.x=%.2f,pv.y=%.2f",pv.x,pv.y);

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: