8.6 Detecting Pinch Gestures

本文介绍了一个使用Objective-C实现的捏放手势案例。通过创建并应用UIPinchGestureRecognizer,该示例详细展示了如何调整UILabel的大小,并记录了捏放操作过程中的关键属性如scale和velocity的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



捏放手势

#import "ViewController.h"


@interface ViewController ()


@property (nonatomic,strong) UIPinchGestureRecognizer *pinchGestureRecognizer;

@property (nonatomic,strong) UILabel *myBlackLabel;

@property (nonatomic,unsafe_unretained) CGFloat currentScale;


@end


@implementation ViewController


- (void) handlePinches:(UIPinchGestureRecognizer*)paramSender{

   NSLog(@"%@ \nscale=%f\nvelocity=%f",paramSender,paramSender.scale,paramSender.velocity);

    if (paramSender.state ==UIGestureRecognizerStateEnded){

       self.currentScale = paramSender.scale;

    }else if (paramSender.state ==UIGestureRecognizerStateBegan && self.currentScale != 0.0f){

        paramSender.scale =self.currentScale;

    }

   if (paramSender.scale !=NAN && paramSender.scale !=0.0){

        paramSender.view.transform =CGAffineTransformMakeScale(paramSender.scale,

                                                                paramSender.scale);

    }

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorwhiteColor];

   CGRect labelRect = CGRectMake(0.0f, 0.0f,200.0f,200.0f);

   self.myBlackLabel = [[UILabelalloc] initWithFrame:labelRect];

    self.myBlackLabel.center =self.view.center;

    self.myBlackLabel.backgroundColor = [UIColorblackColor];

    /* Without this line, the pinch gesture recognizer will not work */

    self.myBlackLabel.userInteractionEnabled =YES;

    [self.viewaddSubview:self.myBlackLabel];

    /* Create the Pinch Gesture Recognizer */

    self.pinchGestureRecognizer = [[UIPinchGestureRecognizeralloc] initWithTarget:self

                                                                           action:@selector(handlePinches:)];

    /* Add this gesture recognizer to the view */

    [self.myBlackLabeladdGestureRecognizer:self.pinchGestureRecognizer];

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


话说CGAffineTransform的那些转换你懂了没,没懂的百度,或是来这里看看。


https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html



打印:

2014-04-10 14:30:22.077 cookbook[1061:907] <UIPinchGestureRecognizer: 0x2008db70; state = Began; view = <UILabel 0x2008c780>; target= <(action=handlePinches:, target=<ViewController 0x1edab020>)>> 

scale=1.003963

velocity=0.005164

2014-04-10 14:30:22.092 cookbook[1061:907] <UIPinchGestureRecognizer: 0x2008db70; state = Changed; view = <UILabel 0x2008c780>; target= <(action=handlePinches:, target=<ViewController 0x1edab020>)>> 

scale=1.010080

velocity=0.099442

。。。。。。


2014-04-10 14:30:23.756 cookbook[1061:907] <UIPinchGestureRecognizer: 0x2008db70; state = Changed; view = <UILabel 0x2008c780>; target= <(action=handlePinches:, target=<ViewController 0x1edab020>)>> 

scale=0.882462

velocity=-0.657148

2014-04-10 14:30:23.772 cookbook[1061:907] <UIPinchGestureRecognizer: 0x2008db70; state = Ended; view = <UILabel 0x2008c780>; target= <(action=handlePinches:, target=<ViewController 0x1edab020>)>> 

scale=0.882462

velocity=-0.657148



velocity 缩小时为负,放大时为正


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值