xcode – 如何使UIImageView可以兼容并使其执行某些 *** 作? (迅速)

xcode – 如何使UIImageView可以兼容并使其执行某些 *** 作? (迅速),第1张

概述(我刚开始使用Swift几天前对编程比较新,所以请耐心等待.)我试图在屏幕上显示随机块,用户必须点击它们才能使它们消失.我已经能够创建块,但我不知道如何实际使它们可以应用.有人可以帮帮我吗?到目前为止这是我的代码: func createBlock(){ let imageName = "block.png" let image = UIImage(named: imageNam (我刚开始使用Swift几天前对编程比较新,所以请耐心等待.)我试图在屏幕上显示随机块,用户必须点击它们才能使它们消失.我已经能够创建块,但我不知道如何实际使它们可以应用.有人可以帮帮我吗?到目前为止这是我的代码:

func createBlock(){    let imagename = "block.png"    let image = UIImage(named: imagename)    let imageVIEw = UIImageVIEw(image: image!)    imageVIEw.frame = CGRect(x: xposition,y: -50,wIDth: size,height: size)    self.vIEw.addSubvIEw(imageVIEw)    UIVIEw.animateWithDuration(duration,delay: delay,options: options,animations: {        imageVIEw.backgroundcolor = UIcolor.redcolor()        imageVIEw.frame = CGRect(x: self.xposition,y: 590,wIDth: self.size,height: self.size)        },completion: { animationFinished in            imageVIEw.removeFromSupervIEw()    })}

这是新代码:

func createBlock(){    let imagename = "block.png"    let image = UIImage(named: imagename)    let imageVIEw = UIImageVIEw(image: image!)    imageVIEw.frame = CGRect(x: xposition,height: size)    self.vIEw.addSubvIEw(imageVIEw)    imageVIEw.userInteractionEnabled = true    let tapRecognizer = UITapGestureRecognizer(target: self,action: Selector("imageTapped"))    imageVIEw.addGestureRecognizer(tapRecognizer)    func imageTapped(gestureRecognizer: UITapGestureRecognizer) {        let tappedImageVIEw = gestureRecognizer.vIEw!        tappedImageVIEw.removeFromSupervIEw()        score += 10    }    UIVIEw.animateWithDuration(duration,animations: {        imageVIEw.backgroundcolor = UIcolor.redcolor()        imageVIEw.frame = CGRect(x: self.xposition,completion: { animationFinished in            imageVIEw.removeFromSupervIEw()    })}
解决方法 创建视图后,需要将其userInteractionEnabled属性设置为true.然后你需要附加一个手势.

imageVIEw.userInteractionEnabled = true//Now you need a tap gesture recognizer//note that target and action point to what happens when the action is recognized.let tapRecognizer = UITapGestureRecognizer(target: self,action: Selector("imageTapped:"))//Add the recognizer to your vIEw.imageVIEw.addGestureRecognizer(tapRecognizer)

现在你仍然需要这个函数,在这种情况下是imageTapped:,这是识别手势时动作发生的地方.已识别的手势将作为参数发送,您可以从手势视图属性中找出从哪个图像视图中点击.

func imageTapped(gestureRecognizer: UITapGestureRecognizer) {    //tappedImageVIEw will be the image vIEw that was tapped.    //dismiss it,animate it off screen,whatever.    let tappedImageVIEw = gestureRecognizer.vIEw!}
总结

以上是内存溢出为你收集整理的xcode – 如何使UIImageView可以兼容并使其执行某些 *** 作? (迅速)全部内容,希望文章能够帮你解决xcode – 如何使UIImageView可以兼容并使其执行某些 *** 作? (迅速)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1069193.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存