简体   繁体   English

动画iOS Objective-C

[英]animations ios objective-c

I have a global variable p. 我有一个全局变量p。 I have a function that draws (or redraws) my complex UIView objects depending on the value of this global variable. 我有一个函数可以根据此全局变量的值绘制(或重新绘制)我的复杂UIView对象。 When I increase p my uiview objects needs to be redrawed. 当我增加p时,我的uiview对象需要重绘。 I need this be done using animation. 我需要使用动画来完成。

double pInitialValue=0.5;
double pNewValue=1.0;

//somewhere before animation we must call [self redraw] to draw our view with p=pInitialValue;

//then, when we call animate function.
//we change p to pNewValue
//and every step must be redrawed using [self redraw]

p=pNewValue; //using animation p must slowly grow from pInitialValue to pNewValue
[self redraw]; //and ofcourse user must see every step of animation so we need to call redraw function

for example I need an animation with duration 4. That means that during this 4 seconds my p must grow from pInitialValue to pNewValue and in every step my redraw function must be called 例如,我需要一个持续时间为4的动画。这意味着在这4秒钟内,我的p必须从pInitialValue增长到pNewValue,并且在每一步中我的重绘函数都必须被调用

Help me, please. 请帮帮我。 How can it be done? 如何做呢?

You can use a timer to increase your p value. 您可以使用计时器来增加p值。 Do that like this: have an instance variable in your class defined like 这样做:在类中定义一个实例变量,如下所示:

NSTimer *timer;

After that just before you want your animation to happen do: 之后,在您希望动画发生之前,请执行以下操作:

CGFloat timerInterval = 1 / 30; // This means 30 frames per second. Change this as you want.
timer = [NSTimer scheduledTimerWithTimeInterval:timerInterval target:self selector:@selector(increaseP) userInfo:nil repeats:YES];

Than have a method called increaseP like this: 不是有一个名为方法increaseP是这样的:

- (void)increaseP
{
    if (p < maxPValue) {
        // Increase p here and redraw your things
    }
    else {
        [timer invalidate];
    }
}

Let me know if you have any question and if this works for you. 让我知道您是否有任何疑问,是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM