UI中的七种手势

 //
// GestureRecognizerViewController.m #import "GestureRecognizerViewController.h"
#import "UIColor+RandomColor.h"
@interface GestureRecognizerViewController ()
{ CGRect _frame; // 用来记录view原来的frame }
@end @implementation GestureRecognizerViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// UIGestureRecognizer 手势识别器,是所有手势识别类的基类,提供了手势识别器的基本功能,有了手势识别器之后,手势的识别全部由这个类来识别,我们就不再关心手势识别的过程,我们只需要关心手势识别之后应该做哪些操作,它的子类有6个,轻拍手势,捏合手势,长按手势,轻扫手势,旋转手势,平移手势,以及平移手势的子类 屏幕边缘手势 UIView *view = [[UIView alloc]initWithFrame:(CGRectMake(, , , ))];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
[view release]; //! 轻拍手势 这种手势用的最多, 跟按钮似的 // UITapGestureRecognizer
/*
// 1. 创建轻拍手势对象
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; // self指视图控制器的对象 // 2. 设置轻拍触发方法时,需要的点击次数 // 一定要在添加手势之前设置
tapGesture.numberOfTapsRequired = 2;
tapGesture.numberOfTouchesRequired = 2; // 3. 向视图对象上添加手势
[view addGestureRecognizer:tapGesture]; // 多态 父类指针指向子类对象
// [view addGestureRecognizer:<#(UIGestureRecognizer *)#>] [tapGesture release];
*/ /**
1. 创建手势识别类的对象(轻拍手势/ 长按手势/ 轻扫手势/ 平移手势/ 捏合手势/ 旋转手势/ 屏幕边缘手势) 2. 设置手势方法的相关属性(如需要几根手指,多长时间才能触发手势事件 等) // 根据需要 3. 向视图对象上添加手势 4. 释放手势对象 */ // 长按手势 UILongPressGestureRecognizer
/*
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
// 设置长按手势最短的触发事件 秒为单位
longPressGesture.minimumPressDuration = 1; [view addGestureRecognizer:longPressGesture]; [longPressGesture release];
*/ // 轻扫手势 UISwipeGestureRecognizer UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; // 设置轻扫手势支持的方法 (默认是向右扫)
// UISwipeGestureRecognizerDirectionDown 向下扫
swipeGesture.direction = UISwipeGestureRecognizerDirectionDown; // 一定要在添加手势之前设置 [view addGestureRecognizer:swipeGesture]; [swipeGesture release]; // 一个视图可以添加多个手势
// 想要实现多个方向轻扫手势: 再添加一个轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
// UISwipeGestureRecognizerDirectionLeft 向左轻扫
swipe.direction = UISwipeGestureRecognizerDirectionLeft; [view addGestureRecognizer:swipe];
[swipe release]; // 平移手势 UIPanGestureRecognizer UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesTure:)]; [view addGestureRecognizer:panGesture];
[panGesture release]; // 捏合手势 UIPinchGestureRecognizer UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)]; // pinchGesture.scale [view addGestureRecognizer:pinchGesture];
[pinchGesture release]; // 旋转手势 // UIRotationGestureRecognizer
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)];
[view addGestureRecognizer:rotationGesture];
[rotationGesture release]; // 屏幕边缘手势 UIScreenEdgePanGestureRecognizer 是UIPanGestureRecognizer的子类 UIScreenEdgePanGestureRecognizer *screenEdgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgeGesture:)]; // 设置屏幕边缘手势支持方法
screenEdgePanGesture.edges = UIRectEdgeRight;
// 必须让位置和屏幕边缘重合
view.frame = CGRectMake(, , , );
// 使每次移动后的view回到原始位置,实现在action方法中
_frame = view.frame; [view addGestureRecognizer:screenEdgePanGesture]; [screenEdgePanGesture release]; }
#pragma mark - 轻拍手势方法
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture { // 获取轻拍手势所在的视图对象
tapGesture.view.backgroundColor = [UIColor randomColor]; } #pragma mark - 长按手势方法
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture { // UIGestureRecognizerStateBegan 当达到条件((longPressGesture.minimumPressDuration = 1;))界限时,触发方法事件
// UIGestureRecognizerStateChanged 当达到条件时,滑动,触发
// UIGestureRecognizerStateEnded 当达到条件,手指离开,触发 // 根据长按手势的状态执行
if (longPressGesture.state == UIGestureRecognizerStateEnded) { longPressGesture.view.superview.backgroundColor = [UIColor randomColor]; } } #pragma mark - 轻扫手势方法
- (void)handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture { swipeGesture.view.backgroundColor = [UIColor randomColor]; } - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe { swipe.view.backgroundColor = [UIColor randomColor];
} #pragma mark - 平移手势方法
- (void)handlePanGesTure:(UIPanGestureRecognizer *)panGesture { // 1. 获取平移增量
CGPoint point = [panGesture translationInView:panGesture.view];
// 2. 让视图的位置发生移动,以上次视图为基准,transform 仿射变换技术 (view上所有点跟随移动) 用了线性代数的知识
panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
//3. 将上次的平移增量置为0
//CGPointZero 代表一个(0, 0)的结构体 CGPointMake(0, 0)
[panGesture setTranslation:CGPointZero inView:panGesture.view]; panGesture.view.backgroundColor = [UIColor randomColor]; } #pragma mark - 捏合手势方法
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture { // 1.根据比例做放射变换, 也是以上次视图为基准 Scale 比例
pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
// 2.将上次的缩放比例置为1
pinchGesture.scale = ; // 或者[pinchGesture setScale:1]; //pinchGesture.view.backgroundColor = [UIColor randomColor]; } #pragma mark - 旋转手势方法
- (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture {
// 1. 根据旋转角度做放射变换,也是以上次视图形变量为基准 rotation 旋转,旋度
rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
// 2. 将上次的旋转角度清零
rotationGesture.rotation = ; } #pragma mark - 边缘手势方法
// 方法和UIPanGestureRecognizer(平移手势)一样
- (void)handleScreenEdgeGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgeGesture { // 手指离开的时候 ,view回到原来的位置
if (screenEdgeGesture.state == UIGestureRecognizerStateEnded) { screenEdgeGesture.view.frame = _frame;
} // 1.获取手指的平移增量
CGPoint point = [screenEdgeGesture translationInView:screenEdgeGesture.view];
// 2.根据平移增量做仿射变换
screenEdgeGesture.view.transform = CGAffineTransformTranslate(screenEdgeGesture.view.transform, point.x, point.y);
// 3. 把平移增量置为0的结构体
[screenEdgeGesture setTranslation:CGPointZero inView:screenEdgeGesture.view]; //screenEdgeGesture.view.frame = _frame;
//NSLog(@"触发了"); } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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

随机颜色

 //
// UIColor+RandomColor.h
// UILessonTouch-04 #import <UIKit/UIKit.h> @interface UIColor (RandomColor)
+ (UIColor *)randomColor;
@end
 //
// UIColor+RandomColor.m
// UILessonTouch-04 #import "UIColor+RandomColor.h"
#define kColorValue arc4random_uniform(256) / 255.0
@implementation UIColor (RandomColor) + (UIColor *)randomColor { return [UIColor colorWithRed:kColorValue green:kColorValue blue:kColorValue alpha:kColorValue]; } @end
上一篇:linux下用cronolog分割apache日志


下一篇:自娱自乐RN版小说APP历程记录