UIView动画混淆了UICollectionView委托 - Swift

时间:2016-04-06 12:36:35

标签: ios iphone swift delegates uicollectionview

UIView.animateWithDuration和UICollectionView Delegate之间是否存在任何已知的异常?

我遇到的问题是,如果我动画UIImageView作为我的UIViewController的背景,我的UICollectionViewCells不可点击(shouldSelectItemAtIndexPath之类的委托方法根本不被调用)

但是如果我注释掉方法调用动画背景,单元格是可点击的,委托方法被调用,一切正常。

提一下,我有一个自定义的UIView类和nib,我加载到我的ViewController上(UICollectionView在这个视图中)。

这是我的代码:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    customView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0] as! CustomView

    customView.frame = childView.bounds
    self.childView.addSubview(customView)
    customView.initialize()

    //self.rotateView()

}


func rotateView() {

    UIView.animateWithDuration(18, delay: 0, options: .CurveLinear, animations: { () -> Void in
        self.backgroundImage1.transform = CGAffineTransformRotate(self.backgroundImage1.transform, CGFloat(M_PI_2))
        }) { (finished) -> Void in
            self.rotateView()
    }
}

这是自定义UIView的initialize()方法:

func initialize(){
    collectionView.backgroundColor = UIColor.clearColor()
    collectionView.registerNib(cellNib, forCellWithReuseIdentifier: "Cell")
    self.collectionView.delegate = self
    self.collectionView.dataSource = self

    ...
}

1 个答案:

答案 0 :(得分:1)

在执行动画时,默认情况下,元素是不可交互的。您可以通过发送.AllowUserInteraction作为选项的一部分来启用它。

试试这个:

func rotateView() {
    UIView.animateWithDuration(18, delay: 0, options: [.CurveLinear,.AllowUserInteration], animations: { () -> Void in
        self.backgroundImage1.transform = CGAffineTransformRotate(self.backgroundImage1.transform, CGFloat(M_PI_2))
        }) { (finished) -> Void in
            self.rotateView()
    }
}