我正在尝试以编程方式构建UI.如何让这个动作起作用?我正在与Swift一起开发.
viewDidLoad中的代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myFirstLabel = UILabel()
let myFirstButton = UIButton()
myFirstLabel.text = "I made a label on the screen #toogood4you"
myFirstLabel.font = UIFont(name: "MarkerFelt-Thin", size: 45)
myFirstLabel.textColor = UIColor.redColor()
myFirstLabel.textAlignment = .Center
myFirstLabel.numberOfLines = 5
myFirstLabel.frame = CGRectMake(15, 54, 300, 500)
myFirstButton.setTitle("?", forState: .Normal)
myFirstButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
myFirstButton.frame = CGRectMake(15, -50, 300, 500)
myFirstButton.addTarget(self, action: "pressed", forControlEvents: .TouchUpInside)
self.view.addSubview(myFirstLabel)
self.view.addSubview(myFirstButton)
}
func …Run Code Online (Sandbox Code Playgroud) 我一直在为 UIView 中的 UIImageView 添加点击手势识别器。我确实为 UIImageView 和 UIView 设置了 isUserInteractionEnabled = true。但它仍然无法正常工作。
@IBOutlet weak var adView: UIView!<br>
@IBOutlet weak var defaultBannerView: UIImageView!
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(FavouritesVC.tappedImageBanner(_:)))
Run Code Online (Sandbox Code Playgroud)
defaultBannerView.addGestureRecognizer(tapGesture) defaultBannerView.isUserInteractionEnabled = true adView.isUserInteractionEnabled = true
func tappedImageBanner(_ sender: UIGestureRecognizer){
print("Tapped")
let tappedImageView = sender.view!
let imageView = tappedImageView as! UIImageView
print(imageView) }
Run Code Online (Sandbox Code Playgroud)
编辑:
我添加了 adView.addGestureRecognizer(tapGesture) 但错误是'无法将'UIView'类型的值转换为'UIImageView''
实际上我正在做 admob,当我无法从 google admob 请求横幅时,我想放置我的默认横幅。所以我希望用户能够点击我的默认横幅并直接访问该网站。那么,当我可以从谷歌获取广告时,我是否应该只在 adView 中添加 tapGesture 并隐藏 imageView?
如果我想在 UIImageView 中添加手势怎么办?我该怎么做?