ios - Pan Segue 调用 viewDidLoad 循环

标签 ios swift uibutton uipangesturerecognizer

我的平移手势以编程方式伪造按钮按下。如果你按下实际的按钮,VC 的 viewDidLoad 会再次运行一次,然后停止,然后就没问题了。 但是当我使用泛手势以编程方式伪造按钮按下时,viewDidLoad 运行大约七次,然后停止。

右边缘手势会触发加载几次的“明天”状态。此时,左边缘手势会触发“今天”状态。这也会加载 4-5 次。

有什么想法吗?如果需要,下面的一些代码。

在 viewDidLoad 中添加了平移手势:

    //Adds edge gesture depending on VC state
    if curMainList == "Today" {
        let panLeftEdgeGesture_MainVC = UIScreenEdgePanGestureRecognizer(target: self, action: "panLeftEdge_toLifeList:")
        panLeftEdgeGesture_MainVC.edges = .Left
        view.addGestureRecognizer(panLeftEdgeGesture_MainVC)

        let panRightEdgeGesture_MainVC = UIScreenEdgePanGestureRecognizer(target: self, action: "panFromRightEdgeAction:")
        panRightEdgeGesture_MainVC.edges = .Right
        view.addGestureRecognizer(panRightEdgeGesture_MainVC)

        self.footer_LeftBtn.addTarget(self, action: "LeftFooterBtnPress", forControlEvents: UIControlEvents.TouchUpInside)
    }
    else {
        let panLeftEdgeGesture = UIScreenEdgePanGestureRecognizer(target: self, action: "panFromLeftEdgeAction:")
        panLeftEdgeGesture.edges = .Left
        view.addGestureRecognizer(panLeftEdgeGesture)
    }

手势触发:

//Gestures visible when view is 'Today'
func panLeftEdge_toLifeList(sender: UIScreenEdgePanGestureRecognizer) {
    dispatch_async(dispatch_get_main_queue()) { () -> Void in
        self.performSegueWithIdentifier("segueToLifeLists", sender: nil)
    }
}

func panFromRightEdgeAction(sender: UIScreenEdgePanGestureRecognizer) {
    //self.tomHdrBtnPress(tomorrowHdr)
    tomorrowHdr.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
}

//Gesture added when viewing 'Tomorrow' list
func panFromLeftEdgeAction(sender: UIScreenEdgePanGestureRecognizer) {
    //self.todayHdrBtnPress(todayHdr)
    todayHdr.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
}

@IBAction func todayHdrBtnPress(sender: AnyObject) {
    UIView.animateWithDuration(0.75, animations: {
        //Header changes
        self.hdrBox.backgroundColor = UIColor(red: 46/255.0, green: 173/255.0, blue: 11/255.0, alpha: 1.0)
        self.footer_RightArrow.hidden = false
        self.footer_RightBtn.hidden = false
        }
        , completion: nil
    )
    curMainList = "TodayTask"
    currentListEntity = curMainList
    viewDidLoad()
}

@IBAction func tomHdrBtnPress(sender: AnyObject) {
    UIView.animateWithDuration(0.75, animations: {
        //Header changes
        self.hdrBox.backgroundColor = UIColor(red: 49/255.0, green: 82/255.0, blue: 171/255.0, alpha: 1.0)
        self.footer_LeftBtn.addTarget(self, action: "LeftFooterBtnPress", forControlEvents: UIControlEvents.TouchUpInside)
        }
        , completion: nil
    )
    curMainList = "TomTask"
    currentListEntity = curMainList
    viewDidLoad()
}

最佳答案

问题是平移手势是连续手势(意味着当您移动手指时,处理程序会被重复调用)。对于平移手势,使用 gesture.state 调用一次的 .Began , 用 state 重复的 .Changed随着用户手指的移动,最后出现了 state.Ended.Cancelled当用户停止手势时。

因此,您通常不会触发 animateWithDuration从一个连续的手势。您只需更新状态,例如,gesture.translationInView()或者你有什么。只需立即更新 View 以反射(reflect)更新后的平移,它被如此频繁地调用,以至于它最终渲染了一些感觉像动画的东西,但它实际上只是一系列更新链接到您的手势识别器的处理程序作为用户接收的连续更新流他们的手势在进步。

唯一一次在连续手势中设置动画是在 .Ended 上或 .Cancelled , 完成动画。例如,也许用户将他们的手指拖到一半,所以 .Changed您只需立即更新 View 而无需任何动画,但是当他们抬起手指时(即手势处理程序收到 .Ended ),您可能会为 View 的最终状态添加动画。

最重要的是,手势识别器中的处理是一种非常不同的动画机制,可能由按钮产生(您可以在其中调用一次 animateWithDuration 方法来启动动画,然后 UIKit 会为您处理动画)。最好不要将这两种截然不同的 UI 更新/动画方式混为一谈。


您还在您的一个 pan 处理程序中执行 segue。如果您想在平移时以交互方式执行 segue,您应该引用 WWDC 2013 视频 Custom Transitions Using View Controllers ,它显示了如何将动画 Controller (指示动画的性质)与交互 Controller (将手势的进度与动画 Controller 联系起来)结合起来。但是在这种模式中,您只执行一次 segue,并且只更新 UIPercentDrivenInteractiveTransition。在手势中,这会将过渡的进度与用户的手指联系起来。 `

关于ios - Pan Segue 调用 viewDidLoad 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34168749/

相关文章:

ios - 删除 UIButton 上禁用的色调

iphone - 我的 UITableViewCell 中没有文字

ios - 检查对象数组中键的值是否与iOS相同?

swift - 我需要帮助在 cloudkit 中保存图像

ios - 创建自定义 UITableView 索引 iOS

ios - 在 iOS 中将文本更改为 uibutton

ios - AWS Lambda/Cognito 身份验证 - 担任 Auth 角色

ios - 用 CAShapeLayer 实现绘制渐变圆弧

ios - 从字典数组中删除重复项,Swift 3

ios - 如何将录制的音频附加到电子邮件中?在 SWIFT