我想创建一个弹出式通知,显示在标签栏后面,动画显示在它上方,然后在从超级视图中删除之前再次返回。
我希望所有这一切都发生在应用代表内部。我有一个功能来做这个,看起来像这样:
func displayPopupNotification(message: String) {
dispatch_async(dispatch_get_main_queue(), {
// get the currently active view controller
var vc = self.window!.rootViewController
if(vc is UINavigationController){
vc = (vc as! UINavigationController).visibleViewController
}
// set up the notification with it's label
let notificationView = UIView()
let label = UILabel()
label.text = message
label.textColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.font = uifont_notification
notificationView.backgroundColor = UIColor.blueColor()
notificationView.translatesAutoresizingMaskIntoConstraints = false
notificationView.alpha = 0 // ready for fading in
label.translatesAutoresizingMaskIntoConstraints = false
// add the notification view to the view controller
vc!.view.addSubview(notificationView)
// add the label to the notification
notificationView.addSubview(label)
// set constraints up
vc!.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(60)]-(-60@999)-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics:nil, views:["subview":notificationView]))
vc!.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics:nil, views:["subview":notificationView]))
notificationView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics:nil, views:["subview":label]))
notificationView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics:nil, views:["subview":label]))
// animate the pop up
UIView.animateWithDuration(0.5, delay:0.5, options: UIViewAnimationOptions.TransitionNone,
animations: {notificationView.alpha = 1.0},
completion: {(value: Bool) in
UIView.animateWithDuration(0.5, delay:2.0, options: UIViewAnimationOptions.TransitionNone, animations: {notificationView.alpha = 0.0},
completion: {(value: Bool) in
notificationView.removeFromSuperview()
})
}
)
})
}
在此代码中,通知会逐渐淡入淡出,因此这是一个良好的开端。这是它的样子:
但是,我真的很喜欢它在标签栏后面,动画,等待一秒钟,然后再次动画下来。
我试图通过改变约束来做到这一点,所以起始约束是这样的:
V:[subview(60)]-(-60@999)-|
然后就像这样动画:
UIView.animateWithDuration(10,
animations: {vc!.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(60)]-0-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics:nil, views:["subview":notificationView]))}
)
但这并没有奏效 - 它只是瞬间赶上第二个约束。
问题:
答案 0 :(得分:-1)
如果在UIView.animateWithDuration
块中设置约束动画,则必须在动画块中调用[view setNeedsLayout];
才能生成动画。
至于获取标签栏后面的视图,您还可以使用不同的方法:将视图添加到内容视图(标签栏上方的视图),然后将其移动到该视图的底部。如果该视图剪辑其内容,则看起来好像视图从标签栏下滑入。