iOS Objective-C -swipe手势

这篇博客介绍了如何在iOS应用中使用Objective-C实现滑动手势。通过创建并设置按钮控件,添加Swipe手势,设置不同位置的button,并为每个button配置点击事件来实现特定操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图
这里写图片描述

需求:

    1、滑动控件,控件动画移动;
    2、点击控件,push进入该控件对应的详细页面;

本demo选取固定的位置rect。
首先,初始化控件的位置,存储成Array列表:

    CGRect rect1 = CGRectMake(375 - 120 - 30, (667- 250)/2 + 25, 120, 200);
    CGRect rect2 = CGRectMake((375 - 100)/2, (667 - 250/3 * 2)/2, 100, 250/3*2);
    CGRect rect3 = CGRectMake(30, (667 - 250)/2+25, 120, 200);
    CGRect rect4 = CGRectMake((375 - 150)/2, (667 - 250)/2, 150, 250);

    _frameArray = @[[NSValue valueWithCGRect:rect1],
                    [NSValue valueWithCGRect:rect2],
                    [NSValue valueWithCGRect:rect3],
                    [NSValue valueWithCGRect:rect4]];

然后初始化原始控件,并且添加swipe手势,因为控件可以点击,我选择button控件:

    for (int i = 0; i < 4; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = [UIColor colorWithRed:(arc4random()%99/99.0) green:(arc4random()%99/99.0) blue:(arc4random()%99/99.0) alpha:0.4];
        [_btnArray addObject:btn];
        [self.view addSubview:btn];

        //添加手势
        UISwipeGestureRecognizer *swipeGesture_right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
        swipeGesture_right.direction = UISwipeGestureRecognizerDirectionRight;
        [btn addGestureRecognizer:swipeGesture_right];

        UISwipeGestureRecognizer *swipeGesture_Left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
        swipeGesture_Left.direction = UISwipeGestureRecognizerDirectionLeft;
        [btn addGestureRecognizer:swipeGesture_Left];
    }

接着给button控件分别设置位置:

for (int i = 0; i < 4; i++) {
        UIButton *btn = _btnArray[i];
        NSValue *tmpValue = _frameArray[i];
        CGRect rect = [tmpValue CGRectValue];
        btn.frame = rect;
    }

最后,触发Swipe手势,button控件动画触发新的frame:

- (void)swipeGesture:(id)sender
{
    UISwipeGestureRecognizer *swipe = sender;

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        //
        UIButton *btn = _btnArray[0];
        [_btnArray removeObjectAtIndex:0];
        [_btnArray addObject:btn];

    }else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        //
        UIButton *btn = _btnArray[3];
        [_btnArray removeObjectAtIndex:3];

        NSMutableArray *array = [NSMutableArray array];
        [array addObject:btn];
        [array addObjectsFromArray:_btnArray];
        _btnArray = array;

    }

    for (int i = 0; i < 4; i++) {
        UIButton *btn = _btnArray[i];
        NSValue *tmpValue = _frameArray[i];
        CGRect rect = [tmpValue CGRectValue];

        if (i == 3) {
            [self.view bringSubviewToFront:btn];
        }

        [UIView animateWithDuration:0.4 animations:^{
            btn.frame = rect;
        }];
    }
}

每一个button可以添加自己的target方法,点击就可以触发相对应的操作了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值