在 Objective C 中使用动画实现 UISlider
Posted
技术标签:
【中文标题】在 Objective C 中使用动画实现 UISlider【英文标题】:Implementing UISlider With Animation in Objective C 【发布时间】:2017-04-08 03:07:17 【问题描述】:我是 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
【问题讨论】:
【参考方案1】:要更改动画的速度,您可以更改持续时间的值。在您的示例中,您的在线持续时间为 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的主要内容,如果未能解决你的问题,请参考以下文章
在 iOS 中使用 Objective C 为 UlLabel 框架周围的边框设置动画
如何在 Objective C 中的 UIScrollView 中禁用动画滚动?
Objective C Animation 我想在屏幕上移动动画