Swift 立即删除 UIview 并在另一个地方显示动画持续时间 1sec
Swift remove UIview immediately and show animation duration 1sec at another place
我想立即从 superview 中删除 UIView 并通过动画在另一个地方显示该视图
我试试这个
mView.removeFromSuperview()
mView.frame = CGRect.init(x: 0, y: 0, width: 121, height: 58)
self.view.addSubview(mView)
UIView.transition(with: self.view, duration: 1, options: [.transitionCrossDissolve], animations: {
self.mView.alpha = 1
}, completion: nil)
此代码将删除 mView 并同时添加 mView 持续时间 1 秒
但我需要立即删除 mView
也许我需要在 mView.removeFromSuperview() 之后更新我的 self.view?
谢谢
我试过了
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
self.infoWindow.alpha = 0
self.view.layoutIfNeeded()
let data = marker.userData as! [String : Any]
let location = CLLocationCoordinate2D(latitude: (data["latitude"] as! Double), longitude: (data["longitude"] as! Double))
self.infoWindow = CustomGMSInfoWindow(frame: CGRect.init(x: 0, y: 0, width: 121, height: 58))
self.infoWindow.center = mapView.projection.point(for: location)
self.infoWindow.center.y = (infoWindow.center.y) - 65
self.infoWindow.frame = CGRect(infoWindow.frame.origin.x, infoWindow.frame.origin.y, infoWindow.frame.width, infoWindow.frame.height)
UIView.animate(withDuration: 1.0) {
self.infoWindow.alpha = 1.0
}
return false
}
运行 动画后:
self.view.layoutIfNeeded()
没有理由删除/重新添加 mView
。您需要做的就是将其 .alpha
设置为零,更改其帧,然后将 .alpha
动画化回 1(将使其“淡入”。
// set alpha to Zero (invisible)
self.mView.alpha = 0.0
// set its new frame
self.mView.frame = CGRect(x: 0, y: 0, width: 121, height: 58)
// "fade in"
UIView.animate(withDuration: 1.0) {
self.mView.alpha = 1.0
}
这是一个完整的例子。每次点击时,红色 mView
会消失,然后在随机位置“淡入”:
class ViewController: UIViewController {
let mView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
mView.backgroundColor = .red
mView.frame = CGRect(x: 100, y: 200, width: 121, height: 58)
view.addSubview(mView)
let tap = UITapGestureRecognizer(target: self, action: #selector(self.doAnim))
view.addGestureRecognizer(tap)
}
@objc func doAnim() -> Void {
let x = CGFloat.random(in: 0...(view.frame.size.width - 121))
let y = CGFloat.random(in: 0...(view.frame.size.height - 58))
// set alpha to Zero (invisible)
self.mView.alpha = 0.0
// set its new frame
self.mView.frame = CGRect(x: x, y: y, width: 121, height: 58)
// "fade in"
UIView.animate(withDuration: 1.0) {
self.mView.alpha = 1.0
}
}
}
编辑:发布的原始问题不是实际问题
这是您发布的代码,有几处更改。通读评论:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
// (1) - if you are creating a NEW view at (3),
// remove this view first
//self.infoWindow.alpha = 0
self.infoWindow.removeFromSuperview()
// (2) - not needed
//self.view.layoutIfNeeded()
let data = marker.userData as! [String : Any]
let location = CLLocationCoordinate2D(latitude: (data["latitude"] as! Double), longitude: (data["longitude"] as! Double))
// (3) - create a NEW view
self.infoWindow = CustomGMSInfoWindow(frame: CGRect.init(x: 0, y: 0, width: 121, height: 58))
self.infoWindow.center = mapView.projection.point(for: location)
self.infoWindow.center.y = (infoWindow.center.y) - 65
// (4) - this is setting the frame to itself - you don't need this
self.infoWindow.frame = CGRect(infoWindow.frame.origin.x, infoWindow.frame.origin.y, infoWindow.frame.width, infoWindow.frame.height)
// (5) - you created a NEW view, so you need to set its alpha here
self.infoWindow.alpha = 0.0
// (6) - and you need to add it as a subview... I'm assuming it needs to be added to the mapView
mapView.addSubview(self.infoWindow)
// (7) if this doesn't fade in the new view...
UIView.animate(withDuration: 1.0) {
self.infoWindow.alpha = 1.0
}
// (8) try it like this - wait 5/100ths of a second before starting the fade-in
UIView.animate(withDuration: 1.0, delay: 0.05, animations: {
self.infoWindow.alpha = 1.0
})
return false
}
我想立即从 superview 中删除 UIView 并通过动画在另一个地方显示该视图
我试试这个
mView.removeFromSuperview()
mView.frame = CGRect.init(x: 0, y: 0, width: 121, height: 58)
self.view.addSubview(mView)
UIView.transition(with: self.view, duration: 1, options: [.transitionCrossDissolve], animations: {
self.mView.alpha = 1
}, completion: nil)
此代码将删除 mView 并同时添加 mView 持续时间 1 秒 但我需要立即删除 mView
也许我需要在 mView.removeFromSuperview() 之后更新我的 self.view?
谢谢
我试过了
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
self.infoWindow.alpha = 0
self.view.layoutIfNeeded()
let data = marker.userData as! [String : Any]
let location = CLLocationCoordinate2D(latitude: (data["latitude"] as! Double), longitude: (data["longitude"] as! Double))
self.infoWindow = CustomGMSInfoWindow(frame: CGRect.init(x: 0, y: 0, width: 121, height: 58))
self.infoWindow.center = mapView.projection.point(for: location)
self.infoWindow.center.y = (infoWindow.center.y) - 65
self.infoWindow.frame = CGRect(infoWindow.frame.origin.x, infoWindow.frame.origin.y, infoWindow.frame.width, infoWindow.frame.height)
UIView.animate(withDuration: 1.0) {
self.infoWindow.alpha = 1.0
}
return false
}
运行 动画后:
self.view.layoutIfNeeded()
没有理由删除/重新添加 mView
。您需要做的就是将其 .alpha
设置为零,更改其帧,然后将 .alpha
动画化回 1(将使其“淡入”。
// set alpha to Zero (invisible)
self.mView.alpha = 0.0
// set its new frame
self.mView.frame = CGRect(x: 0, y: 0, width: 121, height: 58)
// "fade in"
UIView.animate(withDuration: 1.0) {
self.mView.alpha = 1.0
}
这是一个完整的例子。每次点击时,红色 mView
会消失,然后在随机位置“淡入”:
class ViewController: UIViewController {
let mView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
mView.backgroundColor = .red
mView.frame = CGRect(x: 100, y: 200, width: 121, height: 58)
view.addSubview(mView)
let tap = UITapGestureRecognizer(target: self, action: #selector(self.doAnim))
view.addGestureRecognizer(tap)
}
@objc func doAnim() -> Void {
let x = CGFloat.random(in: 0...(view.frame.size.width - 121))
let y = CGFloat.random(in: 0...(view.frame.size.height - 58))
// set alpha to Zero (invisible)
self.mView.alpha = 0.0
// set its new frame
self.mView.frame = CGRect(x: x, y: y, width: 121, height: 58)
// "fade in"
UIView.animate(withDuration: 1.0) {
self.mView.alpha = 1.0
}
}
}
编辑:发布的原始问题不是实际问题
这是您发布的代码,有几处更改。通读评论:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
// (1) - if you are creating a NEW view at (3),
// remove this view first
//self.infoWindow.alpha = 0
self.infoWindow.removeFromSuperview()
// (2) - not needed
//self.view.layoutIfNeeded()
let data = marker.userData as! [String : Any]
let location = CLLocationCoordinate2D(latitude: (data["latitude"] as! Double), longitude: (data["longitude"] as! Double))
// (3) - create a NEW view
self.infoWindow = CustomGMSInfoWindow(frame: CGRect.init(x: 0, y: 0, width: 121, height: 58))
self.infoWindow.center = mapView.projection.point(for: location)
self.infoWindow.center.y = (infoWindow.center.y) - 65
// (4) - this is setting the frame to itself - you don't need this
self.infoWindow.frame = CGRect(infoWindow.frame.origin.x, infoWindow.frame.origin.y, infoWindow.frame.width, infoWindow.frame.height)
// (5) - you created a NEW view, so you need to set its alpha here
self.infoWindow.alpha = 0.0
// (6) - and you need to add it as a subview... I'm assuming it needs to be added to the mapView
mapView.addSubview(self.infoWindow)
// (7) if this doesn't fade in the new view...
UIView.animate(withDuration: 1.0) {
self.infoWindow.alpha = 1.0
}
// (8) try it like this - wait 5/100ths of a second before starting the fade-in
UIView.animate(withDuration: 1.0, delay: 0.05, animations: {
self.infoWindow.alpha = 1.0
})
return false
}