Advertisement
thieumao

Swipe Example

Jul 27th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. #import "SwipeViewController.h"
  4.  
  5. @interface SwipeViewController ()
  6.  
  7. -(void)slideToRightWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer;
  8.  
  9. -(void)slideToLeftWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer;
  10.  
  11. @property (weak, nonatomic) IBOutlet UILabel *lblView;
  12. @property (weak, nonatomic) IBOutlet UIButton *buttonClick;
  13. - (IBAction)buttonClick:(id)sender;
  14.  
  15. @end
  16.  
  17. @implementation SwipeViewController
  18.  
  19. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  20. {
  21.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  22.     if (self) {
  23.         // Custom initialization
  24.     }
  25.     return self;
  26. }
  27.  
  28. - (void)viewDidLoad
  29. {
  30.     [super viewDidLoad];
  31.     // Do any additional setup after loading the view.
  32.     self.lblView.text = @"Test here";
  33.    
  34.     UISwipeGestureRecognizer *swipeRightOrange = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToRightWithGestureRecognizer:)];
  35.     swipeRightOrange.direction = UISwipeGestureRecognizerDirectionRight;
  36.    
  37.     UISwipeGestureRecognizer *swipeLeftOrange = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToLeftWithGestureRecognizer:)];
  38.     swipeLeftOrange.direction = UISwipeGestureRecognizerDirectionLeft;
  39.    
  40.     [self.viewOrange addGestureRecognizer:swipeRightOrange];
  41.     [self.viewOrange addGestureRecognizer:swipeLeftOrange];
  42.    
  43.    
  44.     UISwipeGestureRecognizer *swipeRightBlack = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToRightWithGestureRecognizer:)];
  45.     swipeRightBlack.direction = UISwipeGestureRecognizerDirectionRight;
  46.     [self.viewBlack addGestureRecognizer:swipeRightBlack];
  47.    
  48.    
  49.     UISwipeGestureRecognizer *swipeLeftGreen = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToLeftWithGestureRecognizer:)];
  50.     swipeLeftGreen.direction = UISwipeGestureRecognizerDirectionLeft;
  51.     [self.viewGreen addGestureRecognizer:swipeLeftGreen];
  52.    
  53.     UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideL:)];
  54.     left.direction = UISwipeGestureRecognizerDirectionLeft;
  55.     UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideR:)];
  56.     right.direction = UISwipeGestureRecognizerDirectionRight;
  57.    
  58.     [self.viewSwipe addGestureRecognizer:left];
  59.     [self.viewSwipe addGestureRecognizer:right];
  60. }
  61.  
  62. - (void)didReceiveMemoryWarning
  63. {
  64.     [super didReceiveMemoryWarning];
  65.     // Dispose of any resources that can be recreated.
  66. }
  67.  
  68. /*
  69. #pragma mark - Navigation
  70.  
  71. // In a storyboard-based application, you will often want to do a little preparation before navigation
  72. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  73. {
  74.     // Get the new view controller using [segue destinationViewController].
  75.     // Pass the selected object to the new view controller.
  76. }
  77. */
  78.  
  79.  
  80. -(void)slideToRightWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer{
  81.     [UIView animateWithDuration:0.5 animations:^{
  82.         self.viewOrange.frame = CGRectOffset(self.viewOrange.frame, 320.0, 0.0);
  83.         self.viewBlack.frame = CGRectOffset(self.viewBlack.frame, 320.0, 0.0);
  84.         self.viewGreen.frame = CGRectOffset(self.viewGreen.frame, 320.0, 0.0);
  85.     }];
  86. }
  87.  
  88.  
  89. -(void)slideToLeftWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer{
  90.     [UIView animateWithDuration:0.5 animations:^{
  91.         self.viewOrange.frame = CGRectOffset(self.viewOrange.frame, -320.0, 0.0);
  92.         self.viewBlack.frame = CGRectOffset(self.viewBlack.frame, -320.0, 0.0);
  93.         self.viewGreen.frame = CGRectOffset(self.viewGreen.frame, -320.0, 0.0);
  94.     }];
  95. }
  96.  
  97. -(void)slideR:(UISwipeGestureRecognizer *)gestureRecognizer{
  98.     NSLog(@"Swipe right");
  99. }
  100.  
  101.  
  102. -(void)slideL:(UISwipeGestureRecognizer *)gestureRecognizer{
  103.     NSLog(@"Swipe left");
  104. }
  105.  
  106. bool check;
  107. - (IBAction)buttonClick:(id)sender {
  108. //    self.buttonClick.titleLabel.text = @"Mao";
  109.     if (check) {
  110.         check = NO;
  111.         [self.buttonClick setTitle:@"Check TRUE" forState:UIControlStateNormal];
  112.     } else {
  113.         check = YES;
  114.         [self.buttonClick setTitle:@"Check FALSE" forState:UIControlStateNormal];
  115.     }
  116. }
  117. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement