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

iOS开发从入门到精通--UIGesture手势基础

2016-07-27 09:23 549 查看
UIGesture手势基础



#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

//定义一个视图对象
UIImageView * _imageView;
}

@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

//加载图像对象,从本地加载到内存
UIImage * image =[UIImage imageNamed:@"17_2.png"];

//创建图像视图
_imageView = [[UIImageView alloc]init];

//将图像视图的图像赋值
_imageView.image = image;
_imageView.frame =CGRectMake(50, 80, 200, 300);

[self.view addSubview:_imageView];

//开启交互事件响应开关
//YES:可以响应交互事件
//NO:不能接受响应事件,默认值为NO;
_imageView.userInteractionEnabled=YES;

//创建一个点击手势对象
//UITapGestureRecognizer:点击手势类
//功能:识别点击手势事件
//p1:响应事件的拥有者对象,self表示当前视图控制器
//p2:响应事件的函数
UITapGestureRecognizer * tapOneGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOneAct:)];

//表示手势识别事件的事件类型:几次点击时触发
//默认值为1
tapOneGes.numberOfTapsRequired=1;
//    tapOneGes.numberOfTapsRequired=2;

//表示几个手指点击时触发此事件函数
//默认值为1
tapOneGes.numberOfTouchesRequired=1;

//将点击事件添加到视图中,视图即可响应事件
[_imageView addGestureRecognizer:tapOneGes];

UITapGestureRecognizer * tapTwoGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwoGes:)];

tapTwoGes.numberOfTapsRequired=2;
tapTwoGes.numberOfTouchesRequired=1;
[_imageView addGestureRecognizer:tapTwoGes];

//当单击操作遇到双击操作时,单击操作失效
[tapOneGes requireGestureRecognizerToFail:tapTwoGes];
}

//事件响应函数,单击操作
//参数手势点击事件对象
-(void) tapOneAct:(UITapGestureRecognizer*)tap{
NSLog(@"单击操作!");
//获取手势监控的视图对象
UIImageView * imageView = (UIImageView*)tap.view;
//开始动画过程
[UIView beginAnimations:nil context:nil];
//设置动画过度时间
[UIView setAnimationDuration:2];

imageView.frame=CGRectMake(0, 0, 320, 568);

//结束动画过程
[UIView commitAnimations];

}

//双击操作
-(void)tapTwoGes:(UITapGestureRecognizer*)tap{
NSLog(@"双击操作");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
_imageView.frame =CGRectMake(50, 80, 200, 300);
[UIView commitAnimations];

}

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

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