博客> 6.29 点击进入全屏 解决scrollview点击事件被截获
6.29 点击进入全屏 解决scrollview点击事件被截获
2017-12-11 22:56 评论:0 阅读:1009 qq139391025
UIScrollView UICollectionView

昨天最后遗留的问题解决了,点击cell中的图片进入全屏,再点击就消失.

针对scrollview上的image的touch事件总是被截获的问题,最终决定自定义imageView和scrollview

给imageview增加一个点击事件的协议,在scrollview中实现,(又或者在上一层的collectionView中实现,这一点我还没想好,暂时是在自定义的scrollview中实现,因为我的image还是局部变量)

    //MARK: - 自定义imageview的代理
    protocol CustomImageViewDelegate {

    ///处理图片点击事件
        func tapImageView(sender:AnyObject)
    }

    //MARK: - 自定义imageView
    class CustomImageView: UIImageView {
        var delegate:CustomImageViewDelegate?

       //当有点击事件时执行代理方法
        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            self.delegate?.tapImageView(self)
        }
    }

我发现最近我特别喜欢用didSet...这样我只要调用这个属性的时候会自动执行一些方法,就像我给我的图片数组赋值的时候就创建图片

var picArray:[PhotoModel]?{
    didSet{
        //创建scrollview上的图片
        self.frame = CurrentScreenBounds
        let count = (picArray?.count)! as Int
        for num in 0..&lt;count {
            let frameX = CGFloat.init(num) * currentWidth
            let imgView = CustomImageView()
            imgView.delegate = self
            imgView.frame.size = frame.size
            imgView.contentMode = .ScaleAspectFit
            imgView.userInteractionEnabled = true
            imgView.frame.origin = CGPointMake(frameX, 0)
            imgView.sd_setImageWithURL(NSURL.init(string:(picArray![currentPage!]).thumbnail_pic!), completed:nil)
            self.addSubview(imgView)
        }
    }
}

这样感觉好像挺爽的样子...嘿嘿,不过这里有一点不好,就是这个currentPage 需要先给他赋值,不然数组会取不到数,主要不想在inint里面传值.....

给imageview添加好代理后就实现代理方法----添加点击手势

    //相应点击事件后添加点击手势
func tapImageView(sender: AnyObject) {
    let image = sender as! CustomImageView
    let tap = UITapGestureRecognizer(target: self, action: Selector(clickImage()))
    tap.numberOfTapsRequired = 1
    tap.numberOfTouchesRequired = 1
    image.addGestureRecognizer(tap)
}

再添加手势的方法,点击后一个垃圾淡入淡出的动画移除view

//点击手势方法
func clickImage() {
    //取pageview用于移除
    let window = UIApplication.sharedApplication().keyWindow
    print(window?.subviews.last)
    let pag = window?.subviews.last as! UIPageControl

    //消失动画
    UIView.animateWithDuration(0.5, animations: { 
        self.alpha = 0
        pag.alpha = 0
        }) { bool in
            pag.removeFromSuperview()
            self.removeFromSuperview()
            self.alpha = 1
            pag.alpha = 1
    }
    print("click image")
}

最后再贴一段点击collectionCell后的处理方法,可能有些SB有些乱....

    //MARK: - 点击cell的方法
let pageView = UIPageControl()
let scrollView = CustomScrollView()
//点击cell添加scrollview
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    //scrollview的contentsize根据图片数量决定 宽*数量
    scrollView.contentSize = CGSizeMake(currentWidth * CGFloat.init((picUrl?.count)!), CurrentScreenBounds.height)
    //要先设置currentPage 在设置picArray 不然didSet中无法获取currentPage
    scrollView.currentPage = indexPath.row
    scrollView.picArray = picUrl
    scrollView.delegate = self
    scrollView.contentOffset.x = currentWidth * CGFloat.init(indexPath.row)
    pageView.numberOfPages = (picUrl?.count)!
    pageView.currentPage = indexPath.row
    pageView.userInteractionEnabled = false
    //获取window将两个view贴在最上方
    let window = UIApplication.sharedApplication().windows.last
    pageView.alpha = 0
    scrollView.alpha = 0
    window?.addSubview(scrollView)
    window?.addSubview(pageView)

    //简单的渐变动画
    UIView.animateWithDuration(0.5, animations: {
        self.scrollView.alpha = 1
        self.pageView.alpha = 1
        }) { (bool) in
    }
    //pageview的约束.在底部居中
    pageView.snp_makeConstraints { (make) in
        make.centerX.equalTo(window!)
        make.top.equalTo(currentHeight-50)
        make.width.equalTo(window!).multipliedBy(0.5)
        make.height.equalTo(40)
    }
}

问题真是多啊,道路太坎坷了....我需要基友的力量....

收藏
0
sina weixin mail 回到顶部