简体   繁体   English

删除与imageView一起使用的子视图

[英]delete subview that use with imageView

I have a map picture in my ImageView and I want a red dot for user current location so I use this 3 lines of code for creating red dot above my 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)

all I want is after of 2 seconds of red dot appear it must disappear 我想要的是2秒后出现红点,它必须消失

so I try this 所以我尝试这个

 DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
            self.imageView.delete(overlay)

})

delay function seem work but 延迟功能似乎可以工作,但是

self.imageView.delete(overlay)

return me this error 给我这个错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView delete:]: unrecognized selector sent to instance 0x7f8bef712df0' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[UIImageView删除:]:无法识别的选择器已发送到实例0x7f8bef712df0”

You are getting that error because there is no delete method in the imageView, but there is a method called removeFromSuperview . 之所以会出现此错误,是因为imageView中没有delete方法,但是有一个称为removeFromSuperview

You are going to remove the overlay from the SuperView 您将要从SuperView中删除叠加层

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
   overlay.removeFromSuperview()
})

Or: 要么:

self.overlay.hidden = true

Or try the following if your reference to it was a strong reference, make sure to nil that strong reference: 或者,如果您对它的引用是强引用,请尝试以下操作,请确保nil该强引用:

overlay= nil 

Or animate without using dispatchQueue : 或不使用dispatchQueue设置动画:

UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
    self.overlay.alpha = 0
}, completion: nil)

You need to remove overlay from the superView . 您需要从superView删除overlay You could either do this: 您可以这样做:

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
    overlay.removeFromSuperview()
})

Or if you don´t have overlay globally, you could do this. 或者,如果您没有全局覆盖,则可以执行此操作。 Add a tag to your overlay and then do this: 将标记添加到叠加层,然后执行以下操作:

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()
        }
    }
})

You can remove the dot by using its reference and "removeFromSuperview()" method 您可以通过使用其引用和“ 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()
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM