[ios] 두 숫자 사이에 UILabel 텍스트를 애니메이션합니까?

저는 iPhone 및 Mac 프로그래밍 (이전에 Windows 용으로 개발 됨)을 처음 사용하는데 질문이 있습니다.

Ease-Out 스타일 에서 5 에서 80 까지와 같이 두 숫자 사이 의 text속성을 어떻게 애니메이션 합니까? 가능 합니까? Google에서 한 시간 동안 검색했지만 내 문제를 해결하는 것을 찾지 못했습니다. 내가 원하는 것 : 간단한 게임을 위해 사용자의 돈을 애니메이션합니다. 애니메이션없이 50 에서 100까지 또는 그와 비슷한 수준으로 가면별로 좋지 않습니다 .UILabelCoreAnimation

방법을 알고있는 사람이 있습니까?

감사!



답변

자동 전환을 사용할 수 있습니다. 완벽하게 잘 작동합니다.

// Add transition (must be called after myLabel has been displayed)
 CATransition *animation = [CATransition animation];
animation.duration = 1.0;
animation.type = kCATransitionFade;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[myLabel.layer addAnimation:animation forKey:@"changeTextTransition"];

// Change the text
myLabel.text = newText;

이 코드는 myLabel이 이미 표시된 경우 작동합니다. 그렇지 않으면 myLabel.layer가 nil이되고 애니메이션이 객체에 추가되지 않습니다.


스위프트 4 것이라고 :

let animation: CATransition = CATransition()
animation.duration = 1.0
animation.type = kCATransitionFade
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
myLabel.layer.add(animation, forKey: "changeTextTransition")


답변

잘 작동한다!

목표 -C

[UIView transitionWithView:self.label
                  duration:.5f
                   options:UIViewAnimationOptionCurveEaseInOut |
                           UIViewAnimationOptionTransitionCrossDissolve
                animations:^{

    self.label.text = rand() % 2 ? @"111!" : @"42";

} completion:nil];

스위프트 2

UIView.transitionWithView(label, duration: 0.25, options: [.CurveEaseInOut, .TransitionCrossDissolve], animations: {
    self.label.text = (arc4random() % 2 == 0) ? "111" : "222"
}, completion: nil)

스위프트 3, 4, 5

UIView.transition(with: label, duration: 0.25, options: [.curveEaseInOut, .transitionCrossDissolve], animations: {
    self.label.text = (arc4random() % 2 == 0) ? "111" : "222"
}, completion: nil)


답변

PRTween 이라는 다양한 타이밍 함수로 값을 트위닝하는 훌륭한 엔진을 찾았 습니다 . 클래스를 설치하고 다음 줄을 따라 몇 가지 코드를 만듭니다.

- (IBAction)tweenValue
{
    [[PRTween sharedInstance] removeTweenOperation:activeTweenOperation];
    PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:0.0 endValue:100.0 duration:1.0];
    activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period
                                                             target:self
                                                           selector:@selector(update:)
                                                     timingFunction:&PRTweenTimingFunctionCircOut];
}

- (void)update:(PRTweenPeriod*)period
{
    self.animatingView.center = CGPointMake(period.tweenedValue + 100.0, 200.0);
    self.valueLabel.text = [NSString stringWithFormat:@"%.2f", period.tweenedValue];
}

나를 위해 대접을 작동합니다. 🙂


답변

이전 번호를 밀어내는 새 번호로 카운트 업 및 다운을 원하는 경우 (티커 등) :

let animation = CATransition()
animation.removedOnCompletion = true
animation.duration = 0.2
animation.type = kCATransitionPush
animation.subtype = newValue > value ? kCATransitionFromTop : kCATransitionFromBottom
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
valueLabel.layer.addAnimation(animation, forKey:"changeTextTransition")


답변

Swift 2.0에서는 다음 UIView.transitionWithView()방법을 사용합니다 .

UIView.transitionWithView(self.payPeriodSummaryLabel,
        duration: 0.2,
        options: [.CurveEaseInOut, .TransitionCrossDissolve],
        animations: { () -> Void in
            self.label.text = "your text value"
        }, completion: nil)


답변

또 다른 간단한 대안

extension UILabel {
    func countAnimation(upto: Double) {
        let from: Double = text?.replace(string: ",", replacement: ".").components(separatedBy: CharacterSet.init(charactersIn: "-0123456789.").inverted).first.flatMap { Double($0) } ?? 0.0
        let steps: Int = 20
        let duration = 0.350
        let rate = duration / Double(steps)
        let diff = upto - from
        for i in 0...steps {
            DispatchQueue.main.asyncAfter(deadline: .now() + rate * Double(i)) {
                self.text = "\(from + diff * (Double(i) / Double(steps)))"
            }
        }
    }
}


답변