【问题标题】:How to repeatedly call a method (reload...) with a timer to animate a transition如何使用计时器重复调用方法(重新加载...)以动画转换
【发布时间】:2015-07-09 19:33:09
【问题描述】:

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

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

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

例如:

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

如何为reloadRadialOffsets 设置动画?

【问题讨论】:

  • 你不能真正使用计时器制作流畅的动画。这必须已经在绘图方法中实现。通常你通过创建一个具有动画属性的层来做到这一点。
  • 我更新了问题

标签: ios objective-c animation nstimer core-plot


【解决方案1】:

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

将索引设置为NSNotFound 表示不应选择任何切片。如果您想一次突出显示多个切片,也可以使用一个数组或一组索引。

self.offsetIndex = NSNotFound;

触发动画偏移切片:

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 属性需要一个自定义设置器来触发绘图以更新动画期间的偏移量:

-(void)setSliceOffset:(CGFloat)newOffset
{
    if ( newOffset != sliceOffset ) {
        sliceOffset = newOffset;

        [self.graphs[0] reloadData];
    }
}

【讨论】:

    【解决方案2】:

    您的问题有点令人困惑。您的问题标题为“如何使用计时器重新调用一个方法”,并在您的问题结束时更改为“如何为 reloadRadialOffsets 设置动画?”。

    对于重复调用方法,您可以使用以下任何选项

    选项 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];
        }]; 
     }
    

    这里重复的方法将在动画完成后延迟调用agian。

    当你想停止动画调用时

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

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

    用于动画

    你可以使用

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

    或用户

      CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key];
    

    【讨论】:

    • 很抱歉给您带来了困惑。我感到困惑的原因是,groups.google.com/forum/#!topic/coreplot-discuss/wICF3WNnauU 上写着“你不能用 Core Animation 制作动画,尽管你可能用计时器伪造它并反复重新加载选定的索引。”
    • @Horay 我不知道您是否正在尝试为饼图设置动画。所以在这里发布你的代码。
    【解决方案3】:

    您可以使用以下代码。在这个 UIViewAnimationOptionRepeat 有助于重复动画你想要实现的..

    [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];
    }
    

    希望对您有所帮助...!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 2014-10-02
      相关资源
      最近更新 更多