Animations not working in ios playground

孤者浪人 提交于 2019-12-01 05:19:56

问题


This is my animation method (PreloaderView.swift)

public func performAction(action: PreloaderAction)
{
    let highOpacity = CABasicAnimation.init(keyPath: "opacity")
    let middleOpacity = CABasicAnimation.init(keyPath: "opacity")
    let lowOpacity = CABasicAnimation.init(keyPath: "opacity")

    if action == .PreloaderActionNone {
        self.thirdShape.removeAllAnimations()
        self.firstShape.opacity = 0.3;
        self.secondShape.opacity = 0.5
        self.thirdShape.opacity = 1.0
    } else if action == .PreloaderActionFailure {
        UIView.animateWithDuration(2, animations: {
            self.thirdShape.strokeColor = UIColor.redColor().CGColor
        }) {(completed : Bool) in
            self.thirdShape.strokeColor = self.ringColor.CGColor
        }

    }

    if action == .PreloaderActionStart {

        highOpacity.fromValue = 0.0
        highOpacity.toValue = 1.0
        highOpacity.duration = animationDuration
        highOpacity.autoreverses = true
        highOpacity.repeatCount = .infinity
        self.secondShape.addAnimation(highOpacity, forKey: "highOpacity")

        middleOpacity.fromValue = 0.0
        middleOpacity.toValue = 1.0
        middleOpacity.duration = animationDuration
        middleOpacity.autoreverses = true
        middleOpacity.repeatCount = .infinity
        middleOpacity.beginTime = CACurrentMediaTime() + 0.33
        self.firstShape.addAnimation(middleOpacity, forKey: "middleOpacity")

        lowOpacity.fromValue = 0.0
        lowOpacity.toValue = 1.0
        lowOpacity.duration = animationDuration
        lowOpacity.autoreverses = true
        lowOpacity.beginTime = CACurrentMediaTime() + 0.66
        lowOpacity.repeatCount = .infinity
        self.thirdShape.addAnimation(lowOpacity, forKey: "lowOpacity")

    }
}

This is my playground file

and playground in project navigator

Animations work fine on device, but when I use playground animations not working.


回答1:


Xcode 7, Swift 2

Add

import XCPlayground

at the top of the Playground, then use

XCPlaygroundPage.currentPage.liveView = view

at the bottom of the Playground, where view is the view you want to render.

You also have to open the "Assistant Editor" (in the "View" menu) to see the current live view.

Also, if you're using asynchronous operations, you have to include

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

in order to use them in a Playground.

Xcode 8, Swift 3

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = view


来源:https://stackoverflow.com/questions/37619100/animations-not-working-in-ios-playground

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