在 Objective C 中实现带动画的 UISlider
Implementing UISlider With Animation in Objective C
我是 Objective C 的新手,在使用动画实现 UISlider 时遇到了一些问题。我想使用滑块来更改动画的速度(单击“动画”按钮后图像会改变其大小)但我无法弄清楚如何将滑块与动画的持续时间联系起来。所以我想就如何让它发挥作用征求建议。 (我将滑块的最小值预设为 1,最大值为 5,默认值为 3)。任何建议将不胜感激。这是我的 ViewController.m 代码
//
// ViewController.m
// Project5-TMN
//
// Created by Lab on 4/6/17.
// Copyright © 2017 Toan Nguyen. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *speedValue;
- (IBAction)animate:(id)sender;
- (IBAction)stop:(id)sender;
- (IBAction)speed:(UISlider *)sender;
@end
@implementation ViewController
// Declare two variables to hold the width and height of the image
float w, h;
// Declare a global variable to set the animation status
BOOL animating = YES;
- (void)viewDidLoad {
[super viewDidLoad];
// set w to the size of the image width
w = _image.bounds.size.width;
// set h to the size of the image height
h = _image.bounds.size.height;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)animate:(id)sender {
// set animation status to YES again to let it animating continously after stop
animating = YES;
[UIView animateWithDuration:5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^ {
// Create a CGRect rect, and set the core graphic Rect to the image bound
CGRect rect = _image.bounds;
// Set the width of rect to the difference of the bounds of rect and the image width
rect.size.width = w - rect.size.width;
// Set the height of rect to the difference of the bounds of rect and the image height
rect.size.height = h - rect.size.height;
// Assign rect to the image bounds
_image.bounds = rect;
}
completion:^(BOOL finished) {
if (finished) {
if (animating) {
[self animate:sender];
}
}
}];
}
- (IBAction)stop:(id)sender {
// set animating to NO
animating = NO;
}
- (IBAction)speed:(UISlider *)sender {
UISlider *slider = (UISlider *)sender;
_speedValue.text = [NSString stringWithFormat:@"%.0f", slider.value];
}
@end
要更改动画速度,您可以更改持续时间的值。在您的示例中,您在第 5 行的持续时间恒定为 5 秒:
[UIView animateWithDuration:5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^ {
您可以从滑块重定向值,而不是常量 5。在您的示例中,您只是在标签处显示滑块值,但不要将其用于动画参数。您可以像这样使用此标签的值:
[UIView animateWithDuration:[_speedValue.text intValue] delay:0 options:UIViewAnimationOptionCurveLinear animations:^ {
或者你可以为滑块创建插座并直接使用它的值:
@property (weak, nonatomic) IBOutlet UISlider *speedSlider; //this line at interface should be created by control drag from slider to code
[UIView animateWithDuration:_speedSlider.value delay:0 options:UIViewAnimationOptionCurveLinear animations:^ {
或者您可以使用某种缓冲区变量:
@property (assign, nonatomic) float currentSpeed; // declare at interface
[UIView animateWithDuration:_currentSpeed delay:0 options:UIViewAnimationOptionCurveLinear animations:^ { // at - (IBAction)animate:(id)sender
self.currentSpeed = slider.value; // at - (IBAction)speed:(UISlider *)sender
我是 Objective C 的新手,在使用动画实现 UISlider 时遇到了一些问题。我想使用滑块来更改动画的速度(单击“动画”按钮后图像会改变其大小)但我无法弄清楚如何将滑块与动画的持续时间联系起来。所以我想就如何让它发挥作用征求建议。 (我将滑块的最小值预设为 1,最大值为 5,默认值为 3)。任何建议将不胜感激。这是我的 ViewController.m 代码
//
// ViewController.m
// Project5-TMN
//
// Created by Lab on 4/6/17.
// Copyright © 2017 Toan Nguyen. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *speedValue;
- (IBAction)animate:(id)sender;
- (IBAction)stop:(id)sender;
- (IBAction)speed:(UISlider *)sender;
@end
@implementation ViewController
// Declare two variables to hold the width and height of the image
float w, h;
// Declare a global variable to set the animation status
BOOL animating = YES;
- (void)viewDidLoad {
[super viewDidLoad];
// set w to the size of the image width
w = _image.bounds.size.width;
// set h to the size of the image height
h = _image.bounds.size.height;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)animate:(id)sender {
// set animation status to YES again to let it animating continously after stop
animating = YES;
[UIView animateWithDuration:5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^ {
// Create a CGRect rect, and set the core graphic Rect to the image bound
CGRect rect = _image.bounds;
// Set the width of rect to the difference of the bounds of rect and the image width
rect.size.width = w - rect.size.width;
// Set the height of rect to the difference of the bounds of rect and the image height
rect.size.height = h - rect.size.height;
// Assign rect to the image bounds
_image.bounds = rect;
}
completion:^(BOOL finished) {
if (finished) {
if (animating) {
[self animate:sender];
}
}
}];
}
- (IBAction)stop:(id)sender {
// set animating to NO
animating = NO;
}
- (IBAction)speed:(UISlider *)sender {
UISlider *slider = (UISlider *)sender;
_speedValue.text = [NSString stringWithFormat:@"%.0f", slider.value];
}
@end
要更改动画速度,您可以更改持续时间的值。在您的示例中,您在第 5 行的持续时间恒定为 5 秒:
[UIView animateWithDuration:5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^ {
您可以从滑块重定向值,而不是常量 5。在您的示例中,您只是在标签处显示滑块值,但不要将其用于动画参数。您可以像这样使用此标签的值:
[UIView animateWithDuration:[_speedValue.text intValue] delay:0 options:UIViewAnimationOptionCurveLinear animations:^ {
或者你可以为滑块创建插座并直接使用它的值:
@property (weak, nonatomic) IBOutlet UISlider *speedSlider; //this line at interface should be created by control drag from slider to code
[UIView animateWithDuration:_speedSlider.value delay:0 options:UIViewAnimationOptionCurveLinear animations:^ {
或者您可以使用某种缓冲区变量:
@property (assign, nonatomic) float currentSpeed; // declare at interface
[UIView animateWithDuration:_currentSpeed delay:0 options:UIViewAnimationOptionCurveLinear animations:^ { // at - (IBAction)animate:(id)sender
self.currentSpeed = slider.value; // at - (IBAction)speed:(UISlider *)sender