0
不知道如何簡潔地問這個問題。我有一個動畫圖像。圖像從200x200開始,縮小到0x0,然後再擴大到200x200。我正在使用滑塊來設置圖像的animationDuration。我使用相同的滑塊來設置標籤的文本,並使用由滑塊選擇的值,例如如果滑塊設置爲3,則標籤顯示3.00。當標籤更改文字時,爲什麼會造成圖像超出圖像視圖的邊界?
問題是,當滑塊移動後設置標籤的文本時,圖像將以大於200x200的某個值開始動畫序列。如果我註釋掉設置標籤文本的代碼,我仍然可以更改圖像的動畫速度,並保留在我設置的邊界內。
這裏是ViewController.m文件:
import "ViewController.h"
@interface ViewController()
@property (weak, nonatomic) IBOutlet UIImageView *image;
- (IBAction)animate:(id)sender;
- (IBAction)stopAnimate:(id)sender;
@end
@implementation ViewController
//global variables:
//height and width
float h, w;
//boolean to set default animation status
bool isAnimated = YES;
- (void)viewDidLoad {
[super viewDidLoad];
//set w and h to the width and height of the image, respectively
w = _image.bounds.size.width;
h = _image.bounds.size.height;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)numSliderValueChange:(id)sender {
//when the user moves the slider, show the new value in the lable
[_lblAnimationDuration setText:[NSString stringWithFormat:@"%.2f", [_numSlider value]]];
}
- (IBAction)animate:(id)sender {
//keeps the image animated until stop is clicked
isAnimated = YES;
[UIView animateWithDuration:[_numSlider value] delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
CGRect rect = _image.bounds;
//set w (h) to the difference of the bounds of the rect and the image w (h)
rect.size.width = w - rect.size.width;
rect.size.height = h - rect.size.height;
//assign rect to the image bounds
_image.bounds = rect;
} completion:^(BOOL finished) {
if (finished) {
if (isAnimated) {
[self animate:sender];
}
}
}];
}
- (IBAction)stopAnimate:(id)sender {
//changes isAnimated to NO when "Stop" is clicked, stopping the image's animation
isAnimated = NO;
}
@end
當標籤的文本改變時,圖像將擴大到約一半,應用一個動畫循環。否則,圖像的大小永遠不會超過你在圖片中看到的。
是否使用自動佈局來在Interface Builder中的圖像視圖位置? – dan
@dan是的。限制它被垂直居中 – so8857
你的意思是圖像根本不動畫?或者它動畫到100x100而不是縮小到0x0? –