你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS開發之拖拽手勢

IOS開發之拖拽手勢

編輯:關於IOS

1 前言
利用 UIPanGestureRecognizer 這個手勢識別器, 來實現圖層的拖拽。

2 代碼實例
ZYViewController.m

 
@synthesize helloWorldLabel;
@synthesize panGestureRecognizer;

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
/* Let's first create a label */
CGRect labelFrame = CGRectMake(0.0f, /* X */
0.0f, /* Y */
150.0f, /* 寬 */
100.0f); /* 高 */
self.helloWorldLabel = [[UILabel alloc] initWithFrame:labelFrame];
self.helloWorldLabel.text = @"Hello World";
self.helloWorldLabel.backgroundColor = [UIColor blackColor];
self.helloWorldLabel.textColor = [UIColor whiteColor];
self.helloWorldLabel.textAlignment = NSTextAlignmentCenter;
//確保label可以互動的屬性,以便可以激活其方法
self.helloWorldLabel.userInteractionEnabled = YES;
[self.view addSubview:self.helloWorldLabel];
//創建拖拽手勢
self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePanGestures:)];
//無論最大還是最小都只允許一個手指
self.panGestureRecognizer.minimumNumberOfTouches = 1;
self.panGestureRecognizer.maximumNumberOfTouches = 1;
[self.helloWorldLabel addGestureRecognizer:self.panGestureRecognizer];
}

- (void) handlePanGestures:(UIPanGestureRecognizer*)paramSender{
if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed){
//通過使用 locationInView 這個方法,來獲取到手勢的坐標
CGPoint location = [paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location;
}
}

@synthesize helloWorldLabel;
@synthesize panGestureRecognizer;

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
/* Let's first create a label */
CGRect labelFrame = CGRectMake(0.0f, /* X */
0.0f, /* Y */
150.0f, /* 寬 */
100.0f); /* 高 */
self.helloWorldLabel = [[UILabel alloc] initWithFrame:labelFrame];
self.helloWorldLabel.text = @"Hello World";
self.helloWorldLabel.backgroundColor = [UIColor blackColor];
self.helloWorldLabel.textColor = [UIColor whiteColor];
self.helloWorldLabel.textAlignment = NSTextAlignmentCenter;
//確保label可以互動的屬性,以便可以激活其方法
self.helloWorldLabel.userInteractionEnabled = YES;
[self.view addSubview:self.helloWorldLabel];
//創建拖拽手勢
self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePanGestures:)];
//無論最大還是最小都只允許一個手指
self.panGestureRecognizer.minimumNumberOfTouches = 1;
self.panGestureRecognizer.maximumNumberOfTouches = 1;
[self.helloWorldLabel addGestureRecognizer:self.panGestureRecognizer];
}

- (void) handlePanGestures:(UIPanGestureRecognizer*)paramSender{
if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed){
//通過使用 locationInView 這個方法,來獲取到手勢的坐標
CGPoint location = [paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location;
}
}
運行結果

IOS開發之拖拽手勢
拖拽後結果

IOS開發之拖拽手勢
 

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