删除与 imageView 一起使用的子视图
delete subview that use with imageView
我的 ImageView 中有一张地图图片,我想要一个红点作为用户当前位置,所以我使用这 3 行代码在我的 ImageView 上方创建红点
let overlay: UIView = UIView(frame: CGRect(x: xcorIn * 0.822, y: ycorIn * 1.03, width: 5, height: 5))
overlay.backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
imageView.addSubview(overlay)
我只想要红点出现2秒后就消失
所以我试试这个
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
self.imageView.delete(overlay)
})
延迟功能似乎有效,但
self.imageView.delete(overlay)
return 我这个错误
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[UIImageView delete:]:
unrecognized selector sent to instance 0x7f8bef712df0'
您收到该错误是因为 imageView 中没有 delete
方法,但有一个名为 removeFromSuperview
的方法。
您要从 SuperView 中删除叠加层
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
overlay.removeFromSuperview()
})
或:
self.overlay.hidden = true
或者,如果您对它的引用是强引用,请尝试以下操作,确保 nil
是强引用:
overlay= nil
或者在不使用 dispatchQueue
的情况下制作动画:
UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
self.overlay.alpha = 0
}, completion: nil)
您需要从 superView
中删除 overlay
。您可以这样做:
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
overlay.removeFromSuperview()
})
或者如果您没有全局覆盖,您可以这样做。向叠加层添加标签,然后执行以下操作:
let overlay: UIView = UIView(frame: CGRect(x: 100, y: 100, width: 5, height: 5))
overlay.tag = 0
overlay.backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
imageView.addSubview(overlay)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
for subView in self.imageView.subviews {
if subView.tag == 0 {
subView.removeFromSuperview()
}
}
})
您可以使用其引用和 "removeFromSuperview()" 方法删除点
let overlay: UIView = UIView(frame: CGRect(x: xcorIn * 0.822, y: ycorIn * 1.03, width: 5, height: 5))
overlay.backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
imageView.addSubview(overlay)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
overlay.removeFromSuperview()
})
我的 ImageView 中有一张地图图片,我想要一个红点作为用户当前位置,所以我使用这 3 行代码在我的 ImageView 上方创建红点
let overlay: UIView = UIView(frame: CGRect(x: xcorIn * 0.822, y: ycorIn * 1.03, width: 5, height: 5))
overlay.backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
imageView.addSubview(overlay)
我只想要红点出现2秒后就消失
所以我试试这个
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
self.imageView.delete(overlay)
})
延迟功能似乎有效,但
self.imageView.delete(overlay)
return 我这个错误
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView delete:]: unrecognized selector sent to instance 0x7f8bef712df0'
您收到该错误是因为 imageView 中没有 delete
方法,但有一个名为 removeFromSuperview
的方法。
您要从 SuperView 中删除叠加层
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
overlay.removeFromSuperview()
})
或:
self.overlay.hidden = true
或者,如果您对它的引用是强引用,请尝试以下操作,确保 nil
是强引用:
overlay= nil
或者在不使用 dispatchQueue
的情况下制作动画:
UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
self.overlay.alpha = 0
}, completion: nil)
您需要从 superView
中删除 overlay
。您可以这样做:
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
overlay.removeFromSuperview()
})
或者如果您没有全局覆盖,您可以这样做。向叠加层添加标签,然后执行以下操作:
let overlay: UIView = UIView(frame: CGRect(x: 100, y: 100, width: 5, height: 5))
overlay.tag = 0
overlay.backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
imageView.addSubview(overlay)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
for subView in self.imageView.subviews {
if subView.tag == 0 {
subView.removeFromSuperview()
}
}
})
您可以使用其引用和 "removeFromSuperview()" 方法删除点
let overlay: UIView = UIView(frame: CGRect(x: xcorIn * 0.822, y: ycorIn * 1.03, width: 5, height: 5))
overlay.backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
imageView.addSubview(overlay)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
overlay.removeFromSuperview()
})