UIImageView上的UITapGesture

时间:2015-08-31 08:21:44

标签: ios swift uiimageview

当UIImageView上没有图像时,此代码总是崩溃。

设置点击。

if imageView.gestureRecognizers == nil || imageView.gestureRecognizers!.count == 0 {
    let gestureRecognizer = UITapGestureRecognizer(target: self, action: "fullImage:")
    imageView.addGestureRecognizer(gestureRecognizer)
}

然后,使用照片浏览器。

func fullImage(sender: AnyObject) {
    let attachedView = sender.view!!.superview!
    let images = attachedView.subviews.map{ ($0 as! UIImageView).image! }
    let imageView = (sender as! UIGestureRecognizer).view! as! UIImageView
    let tag = imageView.tag

    let photos = IDMPhoto.photosWithImages(images)
    let browser = IDMPhotoBrowser(photos: photos, animatedFromView: (sender as! UIGestureRecognizer).view!)
    browser.displayToolbar = false
    browser.displayActionButton = false
    browser.displayArrowButton = false
    browser.displayCounterLabel = true
    browser.scaleImage = imageView.image
    browser.usePopAnimation = true
    browser.setInitialPageIndex(UInt(tag - 1))
    ...

有没有什么好方法可以避免无故障?

2 个答案:

答案 0 :(得分:1)

试试这个:

let images = attachedView.subviews.map{ ($0 as? UIImageView).image }.filter { $0 != nil }

这样,如果子视图不是UIImageView,那么你的代码就不会崩溃,你可以过滤掉那些返回的代码。

答案 1 :(得分:0)

尝试将代码放在<p>You can create this using css only <br/>Here is an example:</p> <div class="image-container"> <a class="headline">Your hover text here</a> <img src="http://rmfr-colorado.org/userfiles/704/images/kittens.jpg" /> </div>

if let