ios - 如何一键滑动多个按钮

标签 ios swift

我有 5 个按钮;每个都允许 touchUpInside-action 和 touchDragOutside-action...以及通过 tapGestureRecognizers 的 doubleTap-action 和 longPress-action。

我还想允许用户从任何 UIButton 和滑动触摸到的任何附加 UIButton 开始滑动,这些按钮(包括第一次触摸)执行它们的 @IBAction func Swipe.

所以像这样连续滑动 enter image description here 将为 UIButtons 1、2、3、4 和 5 执行 @IBAction func swipe

最佳答案

你可以尝试这样的事情:

// Create an array to hold the buttons you've swiped over
var buttonArray:NSMutableArray!

override func viewDidLoad() {
    super.viewDidLoad()

    // Make your view's UIPanGestureRecognizer call panGestureMethod:
    // (don't use a UISwipeGestureRecognizer since it's a discrete gesture)
    panGesture.addTarget(self, action: "panGestureMethod:")
}

func panGestureMethod(gesture:UIPanGestureRecognizer) {

    // Initialize and empty array to hold the buttons at the
    // start of the gesture 
    if gesture.state == UIGestureRecognizerState.Began {
        buttonArray = NSMutableArray()
    }

    // Get the gesture's point location within its view
    // (This answer assumes the gesture and the buttons are
    // within the same view, ex. the gesture is attached to
    // the view controller's superview and the buttons are within
    // that same superview.)
    let pointInView = gesture.locationInView(gesture.view)

    // For each button, if the gesture is within the button and
    // the button hasn't yet been added to the array, add it to the
    // array. (This example uses 4 buttons instead of 9 for simplicity's
    // sake
    if !buttonArray.containsObject(button1) && CGRectContainsPoint(button1.frame, pointInView){
        buttonArray.addObject(button1)
    }
    else if !buttonArray.containsObject(button2) && CGRectContainsPoint(button2.frame, pointInView){
        buttonArray.addObject(button2)
    }
    else if !buttonArray.containsObject(button3) && CGRectContainsPoint(button3.frame, pointInView){
        buttonArray.addObject(button3)
    }
    else if !buttonArray.containsObject(button4) && CGRectContainsPoint(button4.frame, pointInView){
        buttonArray.addObject(button4)
    }

    // Once the gesture ends, trigger the buttons within the
    // array using whatever control event would otherwise trigger
    // the button's method.
    if gesture.state == UIGestureRecognizerState.Ended && buttonArray.count > 0 {
        for button in buttonArray {
            (button as UIButton).sendActionsForControlEvents(UIControlEvents.TouchUpInside)
        }
    }
}

(编辑:这是我过去写的一些答案,解释了我所说的 UISwipeGestureRecognizer 是一个离散手势的意思:stackoverflow.com/a/27072281/2274694 , stackoverflow.com/a/25253902/2274694 )

关于ios - 如何一键滑动多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29172160/

相关文章:

ios - 日期格式如 "Monday, December 10"在 ios 5.1 中错误显示

ios - 如何在 iOS 中将大文件 block 明智地上传到 Azure blob 时减少内存使用量?

iphone - 仅在首次打开应用程序时打开的透明覆盖 View

ios - Xcode 更新后无法编译具有 Carthage 依赖项的应用程序

swift - 如何基于 Int 创建一个新的类型安全的原始类型?

arrays - 在 Swift 中向可选数组添加元素

ios - prepareToRecord 崩溃

objective-c - 多个可滚动 subview / TableView

Swift 编译器 2.1.1 永远卡在字典上

ios - 使用 Alamofire 和 SwiftyJSON 正确解析具有多个对象的 JSON 数组