欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ios UISwipeGestureRecognizer 左右滑动隐藏按钮

程序员文章站 2022-04-18 11:31:55
...


#import "loginSuccessViewController.h"

@interface loginSuccessViewController ()
{
    UIButton *_ljBackButton;
}

@end

@implementation loginSuccessViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _ljBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_ljBackButton setFrame:CGRectMake(100, 200, 100, 44)];
    [_ljBackButton setBackgroundColor:[UIColor grayColor]];
    //[_ljBackButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_ljBackButton setTitle:@"测试" forState:UIControlStateNormal];
    [_ljBackButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.view addSubview:_ljBackButton];
    
    // 添加右滑移除手势
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToDirection:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [_ljBackButton addGestureRecognizer:swipeRight];
    
    // 添加右滑移除手势
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToDirection:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionLeft;
    [_ljBackButton addGestureRecognizer:swipeLeft];
}

- (void)swipeToDirection:(UISwipeGestureRecognizer *)swipeGesture
{
    if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight)
    {
        // 向右滑动
        NSLog(@"-----DirectionRight");
        [_ljBackButton setHidden:YES];
    }
    else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        // 向左滑动
        NSLog(@"-----DirectionLeft");
        [_ljBackButton setHidden:YES];
    }
}


@end