【发布时间】: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
【问题讨论】:
标签: ios objective-c uislider