//
// RootViewController.m
// UI_lesson5_class
//
// Created by lirui on 15/10/28.
// Copyright (c) 2015年 Object. All rights reserved.
//
#import "RootViewController.h"
#import "RootView.h"
@interface RootViewController ()
@property(nonatomic,strong)RootView *rootView;
@end
@implementation RootViewController
-(void)loadView{
self.rootView = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
}
- (void)viewDidLoad {
[super viewDidLoad];
//打开 UIImageView 的用户交互
self.rootView.opeImageView.userInteractionEnabled = YES;
//1.轻拍手势
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGRAction:)];
//设置手势的属性
//设置手指的个数
// tapGR.numberOfTouchesRequired = 2;//2个手指
//设置轻拍的次数
// tapGR.numberOfTapsRequired = 2;//双击
//把手势添加到视图上面(手势添加到哪个视图,哪个视图才有效)
[self.rootView addGestureRecognizer:tapGR];
//轻拍 图片回收键盘
[self.rootView.opeImageView addGestureRecognizer:tapGR];
// 2.长按手势
UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGRAction:)];
[self.rootView.opeImageView addGestureRecognizer:longPressGR];
// 3. 旋转手势
UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGRAction:)];
[self.rootView.opeImageView addGestureRecognizer:rotationGR];
// 4.捏合手势
UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGRAction:)];
[self.rootView.opeImageView addGestureRecognizer:pinchGR];
// 5.平移手势
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGRAction:)];
[self.rootView.opeImageView addGestureRecognizer:panGR];
// 6.轻扫手势
UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGRAction:)];
//轻扫手势的方法
//注意:如果想识别2个方向,只能对称搭配(2个同类手势可以绑定同一个方法)
swipeGR.direction =
UISwipeGestureRecognizerDirectionRight|
UISwipeGestureRecognizerDirectionLeft;
[self.rootView.opeImageView addGestureRecognizer:swipeGR];
// 7.屏幕边缘轻扫手势
UIScreenEdgePanGestureRecognizer *screenEdgePanGR = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePanGRAction:)];
screenEdgePanGR.edges = UIRectEdgeLeft;
// screenEdgePanGR.edges = UIRectEdgeRight;
[self.rootView addGestureRecognizer:screenEdgePanGR];
}
#pragma mark 屏幕边缘轻扫手势的实现方法
-(void)screenEdgePanGRAction:(UIScreenEdgePanGestureRecognizer *)sender{
NSLog(@"屏幕边缘轻扫手势");
CGFloat red = (arc4random()%(255 - 0 + 1))/255.0f;
CGFloat blue = (arc4random()%(255 - 0 + 1))/255.0f;
CGFloat green = (arc4random()%(255 - 0 + 1))/255.0f;
self.rootView.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
#pragma mark 轻扫手势的实现方法
-(void)swipeGRAction:(UISwipeGestureRecognizer *)sender{
NSLog(@"轻扫手势");
//UISwipeGestureRecognizer *swipe = sender;
}
#pragma mark 平移手势的实现方法
-(void)panGRAction:(UIPanGestureRecognizer *)sender{
CGPoint point = [sender translationInView:sender.view];
self.rootView.opeImageView.transform = CGAffineTransformTranslate(self.rootView.opeImageView.transform, point.x, point.y);
//平移完成之后,把手势
[sender setTranslation:CGPointZero inView:sender.view];
}
#pragma mark 捏合手势的实现方法
-(void)pinchGRAction:(UIPinchGestureRecognizer *)sender{
// 获取到放大和缩小的 比例 sender.scale
self.rootView.opeImageView.transform = CGAffineTransformScale(self.rootView.opeImageView.transform, sender.scale, sender.scale);
//放大和缩小完毕后,设置当前比例为1
sender.scale = 1;
}
#pragma mark 旋转手势的实现方法
-(void)rotationGRAction:(UIRotationGestureRecognizer *)sender{
NSLog(@"旋转手势的触发方法");
// 直接获取 旋转角度 sender.rotation
self.rootView.opeImageView.transform = CGAffineTransformRotate(self.rootView.opeImageView.transform, sender.rotation);
//每次旋转完成之后旋转角度归0
sender.rotation = 0;
}
#pragma mark 长按手势的触发方法
-(void)longPressGRAction:(UILongPressGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateEnded) {
// NSLog(@"长按手势的触发方法");
//进入 相册
UIImagePickerController *imagePC = [[UIImagePickerController alloc]init];
[self presentViewController:imagePC animated:YES completion:nil];
}
}
#pragma mark 轻拍手势的触发方法
-(void)tapGRAction:(UITapGestureRecognizer *)sender{
NSLog(@"轻拍手势的触发方法");
[self.rootView.textField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
iOS 7大手势
最新推荐文章于 2024-05-06 13:39:24 发布