Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从左侧滑动UIBarButtonItem_Ios_Swift_Animation_Uibarbuttonitem_Uinavigationitem - Fatal编程技术网

Ios 从左侧滑动UIBarButtonItem

Ios 从左侧滑动UIBarButtonItem,ios,swift,animation,uibarbuttonitem,uinavigationitem,Ios,Swift,Animation,Uibarbuttonitem,Uinavigationitem,当转到第二页时,我尝试将后退按钮从左侧滑入0.33秒。现在,动画似乎没有这样做,即使在将持续时间降低到2秒以进行视觉澄清之后。我想知道是否有人能告诉我我在代码中做错了什么,以及如何纠正它 我要说的是,我可能只是误解了动画的某些方面是如何工作的 func addBackButton() { let backButton = UIButton(type: .custom) backButton.setImage(#imageLiteral(resourceName:

当转到第二页时,我尝试将后退按钮从左侧滑入0.33秒。现在,动画似乎没有这样做,即使在将持续时间降低到2秒以进行视觉澄清之后。我想知道是否有人能告诉我我在代码中做错了什么,以及如何纠正它

我要说的是,我可能只是误解了动画的某些方面是如何工作的

func addBackButton() {
        let backButton = UIButton(type: .custom)
        backButton.setImage(#imageLiteral(resourceName: "backArrow"), for: .normal)
        backButton.setTitle("", for: .normal)
        backButton.setTitleColor(backButton.tintColor, for: .normal)
        backButton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        let backBarButtonItem = UIBarButtonItem(customView: backButton)
        backBarButtonItem.customView?.transform = CGAffineTransform(translationX: -backButton.frame.width, y: 0)
        navigationItem.leftBarButtonItems?.insert(backBarButtonItem, at: 0)
        UIView.animate(withDuration: 2, delay: 0.5, animations: {
            backBarButtonItem.customView?.transform = CGAffineTransform(translationX: 2 * backButton.frame.width, y: 0)
        })
    }

您只需将此自定义按钮放在
containerView
中,并将该
containerView
设置为
UIBarButtonItem的
自定义视图。您的设置将如下所示

let containerFrame = CGRect(origin: .zero, size: CGSize(width: 44.0, height: 44.0))
let container = UIView(frame: containerFrame)

let buttonFrame = CGRect(origin: CGPoint(x: -100, y: 0), size: CGSize(width: 44, height: 44))  
let backButton = UIButton(frame: buttonFrame)
backButton.setImage(#imageLiteral(resourceName: "backArrow"), for: .normal)
backButton.setTitle("", for: .normal)
backButton.setTitleColor(backButton.tintColor, for: .normal)
backButton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
container.addSubview(backButton)

let backBarButtonItem = UIBarButtonItem(customView: container)
UIView.animate(withDuration: 2, delay: 0.5, animations: {
    backButton.transform = CGAffineTransform(translationX: 2 * backButton.frame.width, y: 0)
})

您可以检查为什么需要将自定义的
按钮
包装到另一个
UIView

UIBarButtonItem
由于某种原因不是
UIView
子类。如果这是您想要的效果(平台一致性要求您应该使用
UINavigationItem.setLeftBarButtonItems(uu2;:动画:)
),那么您应该将
backButton
添加到视图层次结构中,对其设置动画,然后在完成块中将其设置为
leftBarButtonItem

请记住,您可能需要将其添加到导航栏视图层次结构中,使其显示在其顶部。这种方法还意味着您基本上是在复制导航栏的布局代码,您必须弄清楚它应该在哪里结束

navigationController?.navigationBar.addSubview(backButton)

// layout back button in it's final spot here, either via constraints or frame math

backButton.transform = CGAffineTransform(translationX: -(button.bounds.width + leadingSpacing), translationY: 0.0)

UIView.animate(duration: 0.3, animations: {
    backButton.transform = .identity
} { completed in
    self.navigationItem.leftBarButtonItem = UIBarButtonItem
}

这个答案解决了这个问题,尽管我也对转变身份的帮助性投了“拖延”的赞成票。谢谢你们两位