iOS入门(二十九)Target Action 和手势

Target Action 
关灯游戏    2048
 

//    for (NSString * str  in [UIFont familyNames]) {

//        NSLog(@"%@",str);

//    }

//    NSLog(@"%@",[UIFont familyNames]);




tap  点击

longpress   长按

pan     拖拽

rotation   旋转

pinch    捏合

swipe     清扫

screenEedgePan 屏幕边缘拖拽




 

    UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 50, 280, 420)];

    [self.view addSubview:image];

    [image release];

    [image setImage:[UIImage imageNamed:@"3.jpg"]];

    //打开视图的交互事件

    [image setUserInteractionEnabled:YES];

    //点击手势

    UITapGestureRecognizer  * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

    [image addGestureRecognizer:tap];

    [tap release];

    //设置触发方法所需要的点击次数

    tap.numberOfTapsRequired 3;

    //设置触发点数目

    tap.numberOfTouchesRequired 2;

    

    //长按

    UILongPressGestureRecognizer * longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

    [image addGestureRecognizer:longpress];

    [longpress release];

    //长按有效时间

    longpress.minimumPressDuration 3;

    //允许移动范围

    longpress.allowableMovement 200;

    

    //轻扫

    UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];

    [image addGestureRecognizer:swipe];

    [swipe release];

    //方向

    swipe.direction UISwipeGestureRecognizerDirectionUp |UISwipeGestureRecognizerDirectionDown;

    //旋转

    UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];

    [image addGestureRecognizer:rotation];

    [rotation release];

    //捏合

    UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];

    [image addGestureRecognizer:pinch];

    [pinch release];

    //拖拽

    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];

    [image addGestureRecognizer:pan];

    [pan release];

    

    

    

    

    

    

    

 //    for (NSString * str  in [UIFont familyNames]) {

//        NSLog(@"%@",str);

//    }

//    NSLog(@"%@",[UIFont familyNames]);

}

-(void)tapAction:(UITapGestureRecognizer *)tap

{

    if (self.view.backgroundColor == [UIColor cyanColor]) {

        [self.view setBackgroundColor:[UIColor blackColor]];

    }else{

        [self.view setBackgroundColor:[UIColor cyanColor]];}

}

-(void)longPressAction:(UILongPressGestureRecognizer *)longpress

{

    [self.view setBackgroundColor:[UIColor blueColor]];

//    NSLog(@"%s",__FUNCTION__);

    NSLog(@"按住");

    if (longpress.state == UIGestureRecognizerStateBegan) {

        NSLog(@"长按开始");

    }

    if (longpress.state == UIGestureRecognizerStateEnded) {

        NSLog(@"拜拜~");

    }

}

-(void)swipeAction:(UISwipeGestureRecognizer *)swipe

{

    if (self.view.backgroundColor == [UIColor blueColor]) {

        [self.view setBackgroundColor:[UIColor greenColor]];

    }else{

        [self.view setBackgroundColor:[UIColor blueColor]];}

}

-(void)rotationAction:(UIRotationGestureRecognizer *)rotation

{

    rotation.view.transform CGAffineTransformRotate(rotation.view.transform,rotation.rotation);

    rotation.rotation 0;

}

-(void)pinchAction:(UIPinchGestureRecognizer *)pinch

{

    pinch.view.transform CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);

    pinch.scale 1;

}

-(void)panAction:(UIPanGestureRecognizer *)pan

{

    CGPoint p = [pan translationInView:pan.view];

    pan.view.transform CGAffineTransformTranslate(pan.view.transform, p.x, p.y);

    [pan setTranslation:CGPointZero inView:pan.view];

}

 




 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    if ([_target respondsToSelector:_action]) {

        //没有参数的方法设置

//            [_target performSelector:_action];

        //有参数的方法设置

        [_target performSelector:_action withObject:self];

    }

}

-(void)addTarget:(id)target action:(SEL)action

{

    _target = target;

    _action = action;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值