- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView* view = [[UIView alloc]initWithFrame:self.view.frame];
view.frame = CGRectMake(0, 0, 100, 100);
view.center = self.view.center;
view.backgroundColor = [UIColor redColor];
//1 轻拍 单击,双击,多指点击
//2 长摁
//3 轻扫
//4 旋转
//5 捏合
//6 拖拽
// UITapGestureRecognizer
// //1轻拍
// UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeViewBackground:)];
// tapGesture.numberOfTapsRequired = 2;//点击两次
// tapGesture.numberOfTouchesRequired = 2;//两个手指 模拟器摁住alt模拟
// [view addGestureRecognizer:tapGesture];
//
// //2长摁
//
// UILongPressGestureRecognizer* longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(changeViewBackground:)];
// longpress.minimumPressDuration = 2;
// //当手势结束时也会调用
// [view addGestureRecognizer:longpress];
// //3 清扫
// UISwipeGestureRecognizer* swipeProcess = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(changeViewBackground:)];
// [view addGestureRecognizer:swipeProcess];
// swipeProcess.direction = UISwipeGestureRecognizerDirectionDown;
// /*
// UISwipeGestureRecognizerDirectionDown
// UISwipeGestureRecognizerDirectionUp
// UISwipeGestureRecognizerDirectionRight
// UISwipeGestureRecognizerDirectionLeft
// 以最后的方向
// */
//6 拖拽
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[view addGestureRecognizer:panGesture];//添加到手势
//5 捏合
UIPinchGestureRecognizer * pinGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinAction:)];
[view addGestureRecognizer:pinGesture];
//4 旋转
UIRotationGestureRecognizer* rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[view addGestureRecognizer:rotationGesture];
[self.view addSubview:view];
}
-(void)rotationAction:(UIRotationGestureRecognizer*)rotation{
rotation.view.transform = CGAffineTransformMakeRotation(rotation.rotation);
}
-(void)pinAction:(UIPinchGestureRecognizer*)pin{
pin.view.transform = CGAffineTransformMakeScale(pin.scale, pin.scale);
NSLog(@"%f",pin.scale);
}
-(void)panAction:(UIPanGestureRecognizer*)pan{
CGPoint offsetPoint = [pan translationInView:pan.view];
NSLog(@"%@",NSStringFromCGPoint(offsetPoint));
//让view随着手指拖拽而动
pan.view.transform = CGAffineTransformMakeTranslation(offsetPoint.x, offsetPoint.y);
NSLog(@"%@",@"pan");
}
-(void)changeViewBackground:(UITapGestureRecognizer*)tap{
tap.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 /255.0f green:arc4random() % 256 /255.0f blue:arc4random() % 256 /255.0f alpha:1.0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS 手势(下)
最新推荐文章于 2024-07-07 03:27:59 发布