#Objective - C - UI-design - 第四天 -UIKit框架-UIKit-事件手势-刮刮乐小游戏

响应者

  • iOS中所有能响应事件(触摸、晃动、远程事件)的对象都是响应者。
  • 系统定义了一个抽象的父类UIResponder来表示响应者。 其子类都是响应者。

阻断响应者链

  • 响应者链可以被打断。无法完成检测查询过程。
  • 视图类的属性 : userInteractionEnabled ,关闭后能阻断查询过程。
    //UIView和Label用户交互默认为NO
    //页面多控件尤其复合时,容易使touch被阻断覆盖,将其调整为YES以解决问题
    //用户交互,可开启或阻断事件
    //tv.userInteractionEnabled = NO;
    //UILabel

各种手势

{
//全局变量
    int i ;
    UIImageView *imageView;
    UIImageView *image;
}


//手势初始化设置
#pragma mark- 准备
    image = [[UIImageView alloc]initWithFrame:self.view.bounds];
    image.image = [UIImage imageNamed:@"1.jpg"];
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 150, 150)];//初始化
    [imageView setBackgroundColor:[UIColor redColor]];
    imageView.alpha = .5;
    //层级圆角
    imageView.layer.masksToBounds = YES;
    imageView.layer.borderWidth = 5.;
    imageView.layer.borderColor = [[UIColor blackColor]CGColor];
    imageView.layer.cornerRadius = 15.;
#pragma mark- 必须打开交互开关
    [imageView setUserInteractionEnabled:YES];
    [self.view addSubview:image];
    [self.view addSubview:imageView];

#pragma mark- 轻击,点击(tap)
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapse:)];//创建手势识别对象
    tap.numberOfTapsRequired = 1; //点击次数
    tap.numberOfTouchesRequired = 1; //点击屏幕的手指数
    [imageView addGestureRecognizer:tap];
    //屏幕换背景
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapse1:)];
    [self.view addGestureRecognizer:tap1];

#pragma mark- 捏合,收缩(pinch)
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinse:)];
    [imageView addGestureRecognizer:pinch];

#pragma mark- 旋转(rotate)
    UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rose:)];
    [imageView addGestureRecognizer:rotate];

#pragma mark- 拖动(pan)
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panse:)];
    //手指数最大最小值
    [pan setMinimumNumberOfTouches:1];
    [pan setMaximumNumberOfTouches:1];
    [imageView addGestureRecognizer:pan];

#pragma mark- 长按(longpress)
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longse:)];
    //次数
    longPress.numberOfTapsRequired = 1;
    //手指
    longPress.numberOfTouchesRequired = 1;
    //长按最小时间
    [longPress setMinimumPressDuration:.2f];
    [imageView addGestureRecognizer:longPress];

#pragma mark- 轻扫(swipe)
    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipese:)];
    //指定划的方向
    [swipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
    //2
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipese:)];
    [swipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
    //3
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipese:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    //4
    UISwipeGestureRecognizer *swiperRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipese:)];
    swiperRight.direction = UISwipeGestureRecognizerDirectionRight;
    //放入屏幕
    [self.view addGestureRecognizer:swiperRight];
    [self.view addGestureRecognizer:swipeUp];
    [self.view addGestureRecognizer:swipeLeft];
    [self.view addGestureRecognizer:swipeDown];

#pragma mark- 摇动(shake)
    //摇动手势操作方法,点击硬件设备上Hardware的Shake Gesture按钮即可



//手势触发时方法
#pragma mark- 轻击后实现方法
-(void)tapse:(UITapGestureRecognizer *)tap{
    if (imageView.backgroundColor == [UIColor redColor]) {
        [imageView setBackgroundColor:[UIColor cyanColor]];
    }else{
        imageView.backgroundColor = [UIColor redColor];
    }
}
-(void)tapse1:(UITapGestureRecognizer *)tap{
    if (i > 5) {
        i = 1;
    }
    NSString *string = [NSString stringWithFormat:@"%d.jpg",i++];
    [image setImage:[UIImage imageNamed:string]];
}
#pragma mark- 捏合实现方法
-(void)pinse:(UIPinchGestureRecognizer *)pin{
    if (pin.state == UIGestureRecognizerStateChanged) {//状态改变
        //使用仿射变换来放大缩小
        CGAffineTransform trans = imageView.transform;
        trans = CGAffineTransformScale(trans, pin.scale, pin.scale);
        imageView.transform = trans;
    }
    pin.scale = 1.0;//复位
}
#pragma mark- 旋转(rotate)实现
-(void)rose:(UIRotationGestureRecognizer *)rot{
    if (rot.state == UIGestureRecognizerStateChanged) {
        imageView.transform = CGAffineTransformMakeRotation(rot.rotation);
    }
}
#pragma mark- 拖动实现
-(void)panse:(UIPanGestureRecognizer*)pan{
    if (pan.state == UIGestureRecognizerStateChanged) {
        CGPoint point = [pan translationInView:self.view];//pan相对于屏幕的坐标(移动距离)
        CGPoint center = imageView.center;//要移动物件的中心坐标
        imageView.center = CGPointMake(point.x + center.x, point.y + center.y);
    }
    [pan setTranslation:CGPointZero inView:self.view];//复位
}
#pragma mark- 长按实现
-(void)longse:(UILongPressGestureRecognizer *)lp{
    if (lp.state == UIGestureRecognizerStateChanged) {//长按时间到变黄
        imageView.backgroundColor = [UIColor yellowColor];
    }
    else if(lp.state == UIGestureRecognizerStateEnded){//松开鼠标变回红色
        imageView.backgroundColor = [UIColor redColor];
    }
}
#pragma mark- 轻扫实现
-(void)swipese:(UISwipeGestureRecognizer *)swipe{
    if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
        imageView.backgroundColor = [UIColor yellowColor];
    }else if (swipe.direction == UISwipeGestureRecognizerDirectionDown){
        imageView.backgroundColor = [UIColor cyanColor];
    }else if (swipe.direction == UISwipeGestureRecognizerDirectionLeft){
        imageView.backgroundColor = [UIColor grayColor];
    }else if (swipe.direction == UISwipeGestureRecognizerDirectionRight){
        imageView.backgroundColor = [UIColor brownColor];
        NSLog(@"草那么麻烦");
    }
}
#pragma mark- 摇动(shake)
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    //重写
    if (event.subtype == UIEventSubtypeMotionShake) {
        imageView.image = [UIImage imageNamed:@"5"];
        NSLog(@"草泥马");
    }
}

刮刮乐小游戏

{
        UIImageView *girl;//底图
        UIImageView *clear;//遮罩图
}

    //底图
    girl = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [girl setImage:[UIImage imageNamed:@"001.jpg"]];
    [self.view addSubview:girl];
    //遮罩图
    clear = [[UIImageView alloc]initWithFrame:girl.bounds];
    [clear setImage:[UIImage imageNamed:@"002.jpg"]];
    [self.view addSubview:clear];
//消除实现方法
//手势移动时触发

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //图片覆盖清除事件
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:clear];
    UIGraphicsBeginImageContext(clear.frame.size);
    [clear.image drawInRect:clear.bounds];
    CGContextClearRect(UIGraphicsGetCurrentContext(), CGRectMake(point.x, point.y, 50, 50));
    clear.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值