你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS-滑動手勢添加

IOS-滑動手勢添加

編輯:IOS開發綜合

IOS-滑動手勢添加

 

1.新建一個Single View Application項目,選下一步,項目命名為swipeGestureTest

\

 

\

 

2.修改ViewController類文件

(1)在ViewController.h文件中,添加屬性

 

@property (nonatomic,strong) UISwipeGestureRecognizer *left;

@property (nonatomic,strong) UISwipeGestureRecognizer *right;

@property (nonatomic,strong) UISwipeGestureRecognizer *up;

@property (nonatomic,strong) UISwipeGestureRecognizer *down;

 

@property (nonatomic,strong) UILabel *swipeLabel;


 

(2)在ViewController.m文件中,添加代碼

 

@synthesize left;

@synthesize up;

@synthesize right;

@synthesize down;


 

(3)- (void)viewDidLoad函數中,添加代碼

 

 

self.left=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];

self.left.direction=UISwipeGestureRecognizerDirectionLeft;

[self.view addGestureRecognizer:self.left];

 

 

self.right=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];

self.right.direction=UISwipeGestureRecognizerDirectionRight;

[self.view addGestureRecognizer:self.right];

 

self.up=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];

self.up.direction=UISwipeGestureRecognizerDirectionUp;

[self.view addGestureRecognizer:self.up];

 

self.down=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];

self.down.direction=UISwipeGestureRecognizerDirectionDown;

[self.view addGestureRecognizer:self.down];

 

 

self.swipeLabel=[[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 20)];

self.swipeLabel.text=@"Label";

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

[self.view addSubview:self.swipeLabel];

(4)添加手勢響應函數

 

- (void)handleSwipes:(UISwipeGestureRecognizer *)sender

{

if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {

CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x - 10.0, self.swipeLabel.frame.origin.y);

self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);

self.swipeLabel.text = @"你在往左邊滑動....";

}

if (sender.direction == UISwipeGestureRecognizerDirectionRight) {

CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x + 10.0, self.swipeLabel.frame.origin.y);

self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);

self.swipeLabel.text = @"你在往右邊滑動....";

}

if (sender.direction == UISwipeGestureRecognizerDirectionUp) {

CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x, self.swipeLabel.frame.origin.y-10.0);

self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);

self.swipeLabel.text = @"你在往上邊滑動....";

}

if (sender.direction == UISwipeGestureRecognizerDirectionDown) {

CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x, self.swipeLabel.frame.origin.y+10.0);

self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);

self.swipeLabel.text = @"你在往下邊滑動....";

}

 

}

3 運行

\
源碼鏈接:http://download.csdn.net/detail/liuyinghui523/8491405

 

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