UI第五节——手势

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [UIViewController new];
    [self.window makeKeyAndVisible];

#if 0
    //点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myGes:)];
    
    //双击
    tap.numberOfTapsRequired = 2;
    
    //把点击手势添加到self.window之上
    [self.window addGestureRecognizer:tap];

#endif
    
#if 0
    //放大缩小手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(myGes:)];
    
    [self.window addGestureRecognizer:pinch];
    
#endif
    
#if 0
    //旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(myGes:)];
    
    [self.window addGestureRecognizer:rotation];

#endif
    
    
#if 0
    //滑动手势
    UISwipeGestureRecognizer *swi = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(myGes:)];
    //仅支持水平或者竖直方向其中的一种,以支持水平方向为主
    swi.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
    
    [self.window addGestureRecognizer:swi];
    
#endif
    
#if 0
    //拖动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(myGes:)];
    
    [self.window addGestureRecognizer:pan];
    
#endif
    //长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(myGes:)];
    //2秒之后响应
    //longPress.minimumPressDuration = 2.0f;
    
    [self.window addGestureRecognizer:longPress];
    
    /*
     注意:
     1.要UIView对象可以响应事件,前提是UIView的userInteractionEnabled属性要设置为YES
     2.UILabel和UIImageView这个两个类实例化出来的对象,默认userInteractionEnabled的属性值为NO
     3.视图被隐藏之后,也无法响应事件
     4.如果父视图的userInteractionEnabled的属性值为NO,所有的子视图也一并无法响应事件
     
     */
    
    self.window.userInteractionEnabled = NO;
    

    return YES;
}

//声明一个方法,处理手势事件
-(void)myGes:(UIGestureRecognizer *)ges{
    
    self.window.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
    
    
}

 如果对你有帮助,请关注我哦!

原文地址:https://www.cnblogs.com/laolitou-ping/p/6237667.html