问题
Is there a way in iOS to run a fade animation on a UIView (UILabel specifically) that will fade in with a swipe action (so if it paused half way, the left side is visible, right side invisible and middle some gradient between).
I'm wondering if theres a Gradient mask I could use with the Alpha channel?
Any ideas or code snippets for achieving this sort of reveal? Vertical or Horizontal.
(Link to the same question but for Android for reference - Android - How to fade in a View with a horizontal swipe/fade?)
回答1:
Simple hacky solution will be with additional view which can be placed on top of the label. The way that you can do it is to create this view and to animate it is movement by X axis or Y axis. Still you will need to do two things
- Simple swipe gesture which will trigger the animation.
- Pan Gesture which will move the view with movement of the gesture.(Maybe if you drag more than 50% of the label width/height to trigger the rest of the animation.)
Animation will be better if you add gradient from clear colour to the background colour.
Maybe is not the exact answer of your question but it will work good enough.
As addition you can track your pan gesture like this
@objc private func didPan(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
//dispalay view
case .changed:
let translation = sender.translation(in: view)
//move view by x or y
case .ended,
.cancelled:
//finish animation or reverted
}
default:
break
}
}
You should have such views Parent view - this view will clip subview in his bounds Label - this is your label and it is in the parent Animation view - it is in the parent and it is outside of it is bounds in the beginning of the animation.
来源:https://stackoverflow.com/questions/66210661/ios-how-to-fade-in-a-uiview-with-a-horizontal-swipe-fade