iOS - How to fade in a UIView with a horizontal swipe/fade?

余生颓废 提交于 2021-02-20 02:26:52

问题


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

  1. Simple swipe gesture which will trigger the animation.
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!