1

我们想知道如何使用新按钮轻松构建一个圆形遮罩,将背景和过渡混合到新视图中?请参见此处的示例(观看红色星球被触发):在此处输入图像描述

4

2 回答 2

1

//Swift 4

This is a simple static method for circular bubble transition from a point screen.

//as shown on this link https://github.com/andreamazz/BubbleTransition

    import UIKit

    class AnimationUtility: UIViewController {
        static func animateBubbleTrnsitionView( selfView: UIView, point : CGPoint) {
                //let button = CGRect.init(x: 30, y: selfView.frame.size.height - 15, width: 45, height: 45)
                let button = CGRect.init(x: point.x, y: point.y, width: 0, height: 0)

                let circleMaskPathInitial = UIBezierPath(ovalIn: CGRect.init(x: point.x, y: point.y, width: 1, height: 1))
                let extremePoint = CGPoint(x: point.x, y: 15 - selfView.frame.size.height - 200)
                let radius = sqrt((extremePoint.x*extremePoint.x) + (extremePoint.y*extremePoint.y))
                let circleMaskPathFinal = UIBezierPath(ovalIn: (button.insetBy(dx: -radius, dy: -radius)))

                let maskLayer = CAShapeLayer()
                maskLayer.path = circleMaskPathFinal.cgPath
                selfView.layer.mask = maskLayer

                let maskLayerAnimation = CABasicAnimation(keyPath: "path")
                maskLayerAnimation.fromValue = circleMaskPathInitial.cgPath
                maskLayerAnimation.toValue = circleMaskPathFinal.cgPath
                maskLayerAnimation.duration =  0.9
                maskLayer.add(maskLayerAnimation, forKey: "path")
            }
}

Use this code in following way Perform segue or push without animation.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
            switch segue.identifier! {
            case "navToHomeWithoutanimation":
                self.navigationController?.view.backgroundColor = APP_ORANGE_COLOR  //which ever color you want
                let vc = segue.destination as! MapViewController
                AnimationUtility.animateBubbleTrnsitionView(selfView: vc.view, point: self.view.center)
                break
    }
   }

//The animation will start to animate from given point.

于 2018-03-16T13:29:51.827 回答
0

SWIFT代码 :

我编写了所有代码 appdelegate 文件。使用时间线的屏幕截图而不是完整的 UITableView。

首先,让我们在窗口中添加屏幕截图:

let imageView = UIImageView(frame: self.window!.frame)
imageView.image = UIImage(named: "twitterscreen")
self.window!.addSubview(imageView)


self.mask = CALayer()
self.mask!.contents = UIImage(named: "twitter logo mask").CGImage
self.mask!.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
self.mask!.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.mask!.position = CGPoint(x: imageView.frame.size.width/2, y: imageView.frame.size.height/2)
imageView.layer.mask = mask

动画面具。您会观察到这只鸟的尺寸会先缩小一点,然后再增大,因此只需使用一个动画即可做到这一点,让我们使用 CAKeyframeAnimation。

let keyFrameAnimation = CAKeyframeAnimation(keyPath: "bounds")
keyFrameAnimation.duration = 1
keyFrameAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)]
let initalBounds = NSValue(CGRect: mask!.bounds)
let secondBounds = NSValue(CGRect: CGRect(x: 0, y: 0, width: 90, height: 90))
let finalBounds = NSValue(CGRect: CGRect(x: 0, y: 0, width: 1500, height: 1500))
keyFrameAnimation.values = [initalBounds, secondBounds, finalBounds]
keyFrameAnimation.keyTimes = [0, 0.3, 1]
self.mask!.addAnimation(keyFrameAnimation, forKey: "bounds")

如果您看到结果,请访问 github repo。https://github.com/rounak/TwitterBirdAnimation/

于 2014-09-25T11:30:53.617 回答