访问“<>”符号内的值
Access values within '<>' sign
我有一个如下所示的手势识别器:
@objc func handleTap(_ sender: UITapGestureRecognizer) {
var currentImage = sender.view
print(currentImage)
}
然后打印出来:
Optional(<UIImageView: 0x7ff72601ddd0; frame = (0 0; 414 414); opaque = NO; gestureRecognizers = <NSArray: 0x600000b541e0>; layer = <CALayer: 0x6000005be0c0>>)
如何在 sender.view
中获取 UIImageView
的值?
P.S.
调用点击识别器的代码如下所示:
var tap = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:)))
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 1
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true
}
您可以使用
if let currentImage = sender.view as? UIImageView {
}
或者如果 100% 确定就直接
let currentImage = sender.view as! UIImageView
或
guard let currentImage = sender.view as? UIImageView else { return }
/// then use unwrapped currentImage
我有一个如下所示的手势识别器:
@objc func handleTap(_ sender: UITapGestureRecognizer) {
var currentImage = sender.view
print(currentImage)
}
然后打印出来:
Optional(<UIImageView: 0x7ff72601ddd0; frame = (0 0; 414 414); opaque = NO; gestureRecognizers = <NSArray: 0x600000b541e0>; layer = <CALayer: 0x6000005be0c0>>)
如何在 sender.view
中获取 UIImageView
的值?
P.S.
调用点击识别器的代码如下所示:
var tap = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:)))
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 1
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true
}
您可以使用
if let currentImage = sender.view as? UIImageView {
}
或者如果 100% 确定就直接
let currentImage = sender.view as! UIImageView
或
guard let currentImage = sender.view as? UIImageView else { return }
/// then use unwrapped currentImage