如何用定时器重复调用一个方法(重新加载…)来为一个转换设置animation

我在我的xcode项目中实现了corePlot 。 我试图animation“爆炸”饼图的一部分。 这是我正在使用的方法:

 - (void)radialOffsetForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)idx { if (myIndex == idx) { return 20; } return 0; } 

我有另一种方法调用[pieChart reloadRadialOffset];

例如:

 - (void)thisMethod { [pieChart reloadRadialOffset]; } 

我如何animationreloadRadialOffsets

我刚刚在Plot Gallery示例应用程序的“简单饼图”演示中添加了一个示例。 我向控制器添加了两个属性来保存所选切片的索引和所需的偏移值。 由于偏移量是CGFloat ,因此可以使用Core Animation或CPTAnimation轻松进行animationCPTAnimation

将索引设置为NSNotFound以指示不应select切片。 如果要一次突出显示多个切片,则还可以使用一个数组或一组索引。

 self.offsetIndex = NSNotFound; 

触发animation来偏移切片:

 self.offsetIndex = idx; [CPTAnimation animate:self property:@"sliceOffset" from:0.0 to:35.0 duration:0.5 animationCurve:CPTAnimationCurveCubicOut delegate:nil]; 

绘图数据源需要径向偏移方法:

 -(CGFloat)radialOffsetForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index { return index == self.offsetIndex ? self.sliceOffset : 0.0; } 

sliceOffset属性需要一个自定义设置器来触发该图以在animation期间更新偏移量:

 -(void)setSliceOffset:(CGFloat)newOffset { if ( newOffset != sliceOffset ) { sliceOffset = newOffset; [self.graphs[0] reloadData]; } } 

在你的问题有点混乱。 你的问题的标题是“如何使用计时器简单地调用一个方法”,在你的问题结束时,它会变成“如何为reloadRadialOffset设置animation效果?”。

要反复调用某个方法,可以使用以下任一选项

选项1。

 [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(animatedPie:) userInfo:nil repeats:YES]; 

选项2:

[self performSelector:@seletor(animatePie)withObject:nil afterDelay:1.0];

并在你的方法

 -(void) animatePie { [UIView animateWithDuration:1.0 animations:^ { [pieChart reloadRadialOffsets]; } completion:^(BOOL finished) { [self performSelector:@seletor(animatePie) withObject:nil afterDelay:1.0]; }]; } 

这里重复的方法一旦animation完成就会被延迟地调用。

当你想停止animation调用

  - (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(id)arg 

当使用定时器时,一旦间隔过去,它将被触发。

对于animation

您可以使用

  [UIView animateWithDuration:1.0 animations:^ { } completion:^(BOOL finished) { }]; 

或用户

  CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key]; 

你可以使用下面的代码。 在这个UIViewAnimationOptionRepeat有助于重复animation你想实现的

 [UIView animateWithDuration:5 delay:1 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionRepeat animations:^{ [UIView setAnimationRepeatCount:2]; [pieChart reloadRadialOffsets]; } completion:nil]; 

//你还想使用定时器来调用方法

  NSTimer *animateTimer; animateTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(thisMethod) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:animateTimer forMode:NSDefaultRunLoopMode]; [animateTimer fire]; - (void)thisMethod { [pieChart reloadRadialOffsets]; } 

希望它可以帮助你…!