ios - 制作一个滑动按钮

标签 ios

我想制作一个滑动按钮,因此当用户从右向左滑动按钮时,按钮也会从右向左绘制,最后它将插入另一个 View ,任何人都可以提供示例代码。

最佳答案

// make button  

UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 [btn setFrame:CGRectMake(50, 50, 50, 50)];
 [btn addTarget:self action:@selector(wasDragged:withEvent:)
              forControlEvents:UIControlEventTouchDragInside];
 [self.view addSubview:btn];


// here swipe buttons


- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
    // get the touch
    UITouch *touch = [[event touchesForView:button] anyObject];

    // get delta
    CGPoint previousLocation = [touch previousLocationInView:button];
    CGPoint location = [touch locationInView:button];
    CGFloat delta_x = location.x - previousLocation.x;
    CGFloat delta_y = location.y - previousLocation.y;

    // move button
    CGPoint moveBtnCenter = CGPointMake(button.center.x + delta_x,
                                        button.center.y + delta_y);
    button.center = moveBtnCenter;

    NSArray* allSubViews = [self.view subviews];
    for (UIView*subView in allSubViews)
    {
        if ([subView isKindOfClass:[UIButton class]])
        {
            CGPoint subViewCenter = subView.center;
            if (CGPointEqualToPoint(moveBtnCenter, subViewCenter))
            {
                subView.center = previousLocation;
            }
        }
    }
}

关于ios - 制作一个滑动按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9166465/

相关文章:

iphone - shouldStartLoadWithRequest 附加链接与 applewebdata

ios - 使用 pod 中的测试代码在物理设备上运行 Xcode ui 测试

ios - KIF 测试用例在 UIView-KIFAdditions.m 类 iOS 崩溃

ios - 如何通过for循环加载选择器 View ?

ios - 为什么只有第一个图没有用形状标记,与图中的其余图不同?

ios - 在设备上运行时出现 xcode 错误

android - 移动内容引用应用程序

ios - Swift - 核心数据关系 - 专辑风格

iOS App Store - 当应用程序为 "Waiting for Review"时,您可以传输应用程序吗

ios - 如何在初始化 AVURLAsset 时根据 AVURLAssetHTTPCookiesKey 设置正确的 cookie 值