捏放手势
#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 缩小时为负,放大时为正