IOS-滑動手勢添加

來源:互聯網
上載者:User

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.