你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS開發之放大和縮小的手勢監聽和處理

IOS開發之放大和縮小的手勢監聽和處理

編輯:關於IOS

1 前言
UIPinchGestureRecognizer捕獲並處理類似放大和縮小的手勢動作。

2 代碼實例
ZYViewController.m

 
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
CGRect labelRect = CGRectMake(0.0f, /* X */
0.0f, /* Y */
200.0f, /* Width */
200.0f); /* Height */
self.myBlackLabel = [[UILabel alloc] initWithFrame:labelRect];
self.myBlackLabel.center = self.view.center;
self.myBlackLabel.backgroundColor = [UIColor blackColor];
/* 使UIPinchGestureRecognizer聚拉的手勢的放大縮小功能生效 */
self.myBlackLabel.userInteractionEnabled = YES;
[self.view addSubview:self.myBlackLabel];
self.pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePinches:)];
[self.myBlackLabel addGestureRecognizer:self.pinchGestureRecognizer];
}

- (void) handlePinches:(UIPinchGestureRecognizer*)paramSender{
//UIPinchGestureRecognizer其中有兩個比較重要的變量 scale 和 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
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
CGRect labelRect = CGRectMake(0.0f, /* X */
0.0f, /* Y */
200.0f, /* Width */
200.0f); /* Height */
self.myBlackLabel = [[UILabel alloc] initWithFrame:labelRect];
self.myBlackLabel.center = self.view.center;
self.myBlackLabel.backgroundColor = [UIColor blackColor];
/* 使UIPinchGestureRecognizer聚拉的手勢的放大縮小功能生效 */
self.myBlackLabel.userInteractionEnabled = YES;
[self.view addSubview:self.myBlackLabel];
self.pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePinches:)];
[self.myBlackLabel addGestureRecognizer:self.pinchGestureRecognizer];
}

- (void) handlePinches:(UIPinchGestureRecognizer*)paramSender{
//UIPinchGestureRecognizer其中有兩個比較重要的變量 scale 和 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);
}
}
運行結果

IOS開發之放大和縮小的手勢監聽和處理
放大後結果

 

IOS開發之放大和縮小的手勢監聽和處理

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved