jailani
jailani

Reputation: 2270

Adding subviews with time delay on UISlider?

I have a UISlider and I am trying to add subviews dynamically on the track of the UISlider with animation. I need 1 second time delay before adding each subview. How can I achieve this?

Upvotes: 0

Views: 571

Answers (3)

Win Coder
Win Coder

Reputation: 6756

you can hook up the valueChangeEvent with an action method.

You can put an animation block in that method with passing the seconds to wait before starting an animation as delay parameter.

[UIView animateWithDuration:(NSTimeInterval) delay:(NSTimeInterval) options:(UIViewAnimationOptions) animations:^(void)animations completion:^(BOOL finished)completion]

then you can change add the view with whatever animation your require.

Upvotes: 0

Anil
Anil

Reputation: 2438

This is a basic setup of how you can go about achieving it:

@interface ViewController () {
    NSTimer *_timer;
    CGRect sliderFrame;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.slider.minimumValue = 0.0f;
    self.slider.maximumValue = 100.0f;

    sliderFrame = self.slider.frame;

    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(addLabel:) userInfo:nil repeats:YES];
    [_timer fire];
}

- (void) addLabel:(NSTimer*) timer {      
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(sliderFrame.origin.x + self.slider.value/100 * self.slider.frame.size.width, sliderFrame.origin.y - 10, 20, 20)];
    label.text = @"1";
    [self.view addSubview:label];
}

@end

Upvotes: 1

Gismay
Gismay

Reputation: 814

Assuming you are using an animation block, then, within your block you can use setAnimationDelay. For example;

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

This would do a 5 second animation with a delay of 1 second before it started.

Upvotes: 0

Related Questions