UI第五节——手势

本文详细介绍了iOS中各种手势识别器的使用方法,包括点击、滑动、旋转等,并提供了具体的实现示例。同时,还强调了UIView对象响应事件的前提条件。
#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值