How to repeatedly call a method (reload…) with a t

2019-09-21 02:17发布

I implemented corePlot in my xcode project. I'm trying to "explode" a slice of the pie chart with animation. Here is the method I'm using:

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

I have another method which calls [pieChart reloadRadialOffset];.

For example:

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

How can I animate the reloadRadialOffsets?

3条回答
萌系小妹纸
2楼-- · 2019-09-21 02:49

In your question is bit confusing. Your question title says "how to reaptedly call a mathod using timer" and at the end of your question it changes to "How can I animate the reloadRadialOffsets?".

For repeatedly calling a method you can use any of the following options

option 1.

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

option 2:

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

and in your method

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

Here the repeated method will be called agian with a delay once the animation is complete.

When you wanted to stop the animation call

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

When timer is used it will be fired once the interval is elapsed.

For animation

You can use

    [UIView animateWithDuration:1.0 animations:^
    {

    } completion:^(BOOL finished) {

    }];

or user

  CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key];
查看更多
一纸荒年 Trace。
3楼-- · 2019-09-21 03:00

You can use below code. In this UIViewAnimationOptionRepeat is helpful for repeat animation what you want to achieve..

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

                 }
                 completion:nil];

//Still you want to call method using timer

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

Hope it helps you...!

查看更多
叛逆
4楼-- · 2019-09-21 03:10

I just added an example to the "Simple Pie Chart" demo in the Plot Gallery example app. I added two properties to the controller to hold the index of the selected slice and the desired offset value. Since the offset is a CGFloat it is easily animated using Core Animation or CPTAnimation.

Set the index to NSNotFound to indicate that no slice should be selected. You could also use an array or set of indices if you want to highlight more than one slice at a time.

self.offsetIndex = NSNotFound;

Trigger the animation to offset the slice:

self.offsetIndex = idx;

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

The plot datasource needs the radial offset method:

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

The sliceOffset property needs a custom setter to trigger the plot to update the offsets during the animation:

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

        [self.graphs[0] reloadData];
    }
}
查看更多
登录 后发表回答