UICollectionViewCell Shake

后端 未结 5 819
谎友^
谎友^ 2020-12-13 19:00

I have created a UICollectionView and would like to have all the cells shake like the edit mode of the springboard on the iPhone. I have created my shake code but don\'t kno

5条回答
  •  星月不相逢
    2020-12-13 19:49

    If anyone is curious on how to do this in Swift 2.0 the below should be a good starting point.

    let animationRotateDegres: CGFloat = 0.5
    let animationTranslateX: CGFloat = 1.0
    let animationTranslateY: CGFloat = 1.0
    let count: Int = 1
    
    func wobble() {
        let leftOrRight: CGFloat = (count % 2 == 0 ? 1 : -1)
        let rightOrLeft: CGFloat = (count % 2 == 0 ? -1 : 1)
        let leftWobble: CGAffineTransform = CGAffineTransformMakeRotation(degreesToRadians(animationRotateDegres * leftOrRight))
        let rightWobble: CGAffineTransform = CGAffineTransformMakeRotation(degreesToRadians(animationRotateDegres * rightOrLeft))
        let moveTransform: CGAffineTransform = CGAffineTransformTranslate(leftWobble, -animationTranslateX, -animationTranslateY)
        let conCatTransform: CGAffineTransform = CGAffineTransformConcat(leftWobble, moveTransform)
    
        transform = rightWobble // starting point
    
        UIView.animateWithDuration(0.1, delay: 0.08, options: [.AllowUserInteraction, .Repeat, .Autoreverse], animations: { () -> Void in
            self.transform = conCatTransform
            }, completion: nil)
    }
    
    func degreesToRadians(x: CGFloat) -> CGFloat {
        return CGFloat(M_PI) * x / 180.0
    }
    

    When I want to make my cells wobble I do so like this:

    for cell in collectionView.visibleCells() {
            let customCell: MyCustomCell = cell as! MyCustomCell
            customCell.wobble()
        }
    

提交回复
热议问题