2012-12-15 59 views
7

tôi muốn tạo hiệu ứng động cho UISlider cho ví dụ: 0,25 đến 0,75 và ngược lại. Điều này sẽ cho người dùng biết phải làm gì.Animate UISlider trơn tru

tôi đã cố gắng này:

[self incrementCounter:[NSNumber numberWithInt:0]]; 


-(void) incrementCounter:(NSNumber *)i { 
    [Slider setValue:[i floatValue]/1000]; 
    [self performSelector:@selector(incrementCounter:) withObject:[NSNumber numberWithInt:i.intValue+1] afterDelay:0.001]; 
} 

nhưng thats không quá trơn tru ... tôi có thể sử dụng hiệu ứng chuyển tiếp cho việc này?

[Slider setValue:1 animated:YES]; 

là để nhanh chóng ...

[UIView animateWithDuration:2.0 
       animations:^{ 
        [Slider setValue:0.2]; 
       }]; 
[UIView animateWithDuration:2.0 
       animations:^{ 
        [Slider setValue:0.5]; 
       }]; 

Chỉ cần sinh động một giây ...

Trả lời

14

Bạn cần phải chuỗi các hình ảnh động bằng cách đặt các hình ảnh động thứ hai trong khối hoàn thành đầu tiên :

-(IBAction)animateSlider:(id)sender { 

    [UIView animateWithDuration:2 animations:^{ 
     [self.slider setValue:.75]; 
    } completion:^(BOOL finished) { 
     [UIView animateWithDuration:2 animations:^{ 
      [self.slider setValue:0.25];//or `[self.slider setValue:(.75)-(.5*finished)];` if you want to be safe 
     }]; 
    }]; 
} 
2

Swift 2.2 version for rdelmar 'S trả lời

@IBOutlet weak var slider: UISlider! 

func animateSlider(sender: AnyObject){ 
    UIView.animateWithDuration(2, animations: {() in 
     self.slider.setValue(0.75, animated: true) 
     }, completion:{(Bool) in 
      UIView.animateWithDuration(2, animations: {() in 
       self.slider.setValue(0.25, animated: true) 
      }) 
    }) 
} 

và để gọi hàm vượt qua UISlider trong tham số

animateSlider(self.slider)