简体   繁体   中英

Swift: UITableViewCell bounce from right to left animation

I have been trying for weeks now to implement a bounce from right to left animation of the cell in the tableview when the user taps on the cell just like the Instagram and Snapchat iOS apps have.

Due to my lack of experience with animations in Swift and after weeks of research trying to find some information on how to implement such an animation, I haven't been able to figure out how this could be done.

Where should a beginner to animations in Swift look to learn about how to create such an animation or any useful animation (ie on button taps) that creates a better user experience?

I appreciate any help. Thank you.

Swift 4:

let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.05
animation.repeatCount = 3
animation.autoreverses = true
animation.speed = 0.8
animation.fromValue = NSValue(cgPoint: CGPoint(x: cell.center.x - 3, y: cell.center.y))
animation.toValue = NSValue(cgPoint: CGPoint(x: cell.center.x + 3, y: cell.center.y))
cell.layer.add(animation, forKey: "position")

Look into Core Animation: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html

Here's an example of animating a cell on tap/selection from side to side.

let animation = CABasicAnimation(keyPath: "position")
    animation.duration = 0.05
    animation.repeatCount = 3
    animation.autoreverses = true
    animation.speed = 0.8
    animation.fromValue = NSValue(CGPoint: CGPointMake(selectedCell!.center.x - 3, selectedCell!.center.y))
    animation.toValue = NSValue(CGPoint: CGPointMake(selectedCell!.center.x + 3, selectedCell!.center.y))
    selectedCell!.layer.addAnimation(animation, forKey: "position")

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM