注意:(三个手指实现拖拽功能,按住option按钮和三个手指实现旋转功能)
首先创建一个single类型的类
在根视图控制器的.h文件中添加以下代码
以下程序适合x cold4.6.3
#import <UIKit/UIKit.h>
@interface liViewController : UIViewController
@property (retain,nonatomic) UIView *aNewView;
@end
在.m文件中添加以下代码
@implementation liViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 120, 120)];
tempView.backgroundColor = [UIColor purpleColor];
self.aNewView = tempView;
[tempView release];
[self.view addSubview:self.aNewView];
//添加各种手势以及其关联动作
//1、添加单击手势
UITapGestureRecognizer *singletap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];
[self.view addGestureRecognizer:singletap];
[singletap release];
//添加双击手势 如果什么也不写下边新的对象以及关联的动作会把前边的动作覆盖住,不管单击还是双击都会运行后一个执行的动作
UITapGestureRecognizer *doubletap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];
//为了做标识,证明是双击 为了和上边的单击操作做区别
doubletap.numberOfTapsRequired = 2;
//如果没有这句话会同时执行单击和双击操作
[singletap requireGestureRecognizerToFail:doubletap];
[self.view addGestureRecognizer:doubletap];
[doubletap release];
//添加长按手势
UILongPressGestureRecognizer *longtap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressed:)];
//设置长按的时间是两秒
longtap.minimumPressDuration = 2;
[self.view addGestureRecognizer:longtap];
[longtap release];
//添加扫除手势
UISwipeGestureRecognizer *swiptap = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipGesture:)];
swiptap.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swiptap];
[swiptap release];
//添加平移的手势
UIPanGestureRecognizer *translationTap = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(translation:)];
[self.view addGestureRecognizer:translationTap];
[translationTap release];
//添加旋转的手势
UIRotationGestureRecognizer *rotationTap = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[self.view addGestureRecognizer:rotationTap];
[rotationTap release];
}
//单击关联的响应
- (void)singleGesture:(UITapGestureRecognizer *)tap
{
NSLog(@"单击");
}
//双击关联的响应
- (void)doubleGesture:(UITapGestureRecognizer *)tap
{
NSLog(@"双击");
}
//长按
- (void)longPressed:(UILongPressGestureRecognizer *)pressed
{
NSLog(@"长按");
}
//清除手势关联的响应
- (void)swipGesture:(UISwipeGestureRecognizer *)swip
{
NSLog(@"清除");
}
//平移手势对应的响应动作
- (void)translation:(UIPanGestureRecognizer *)pan
{
CGPoint point = [pan locationInView:self.view];
self.aNewView.center = point;
NSLog(@"%@",NSStringFromCGPoint(point));
// NSLog(@"平移");
}
//旋转手势对应的响应动作
- (void) rotation:(UIRotationGestureRecognizer *)revolve
{
NSLog(@"旋转");
float degree = revolve.rotation * (180/M_PI);
NSLog(@"旋转的角度是%f",degree);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[_aNewView release];
[super dealloc];
}
@end