【发布时间】:2017-02-04 23:39:18
【问题描述】:
我是 Objective-C 的新手。我创建了一个向上移动 3 个按钮的动画。这些按钮包含图像。但是,当此按钮动画发生时,它会调整按钮图像的大小并使它们变得巨大。我试图更正下面的代码以调整按钮图像的大小,但我仍然得到巨大的按钮。有人可以帮帮我吗?它应该是宽度:78 高度:76。我尝试替换宽度和高度,但它仍然不起作用。注意:只需更正代码,我不需要完全不同的答案。
-(IBAction)Search:(id)sender {
CGFloat screenWidth = self.view.bounds.size.width;
CGFloat screenHeight = self.view.bounds.size.height;
CGFloat normalizedX = (124 / 320); // You calculate these 'normalized' numbers, possibly from a designer's spec.
// it's the percent the amount should be over, as number from 0-1.
// This number is based on screen width of 320 having x 124 pt.
CGFloat startingX = normalizedX * screenWidth;
CGFloat startingY = (475 / 200) * screenHeight;
CGFloat width = (42 / 40) * screenWidth;
CGFloat height = (30 / 30) * screenHeight;
CGRect startingRect = CGRectMake(startingX, startingY, width, height);
self.button.frame = startingRect;
self.buttonTwo.frame = startingRect;
self.buttonThree.frame = startingRect;
// animate
[UIView animateWithDuration:0.75 animations:^{
CGFloat firstX = (13 / 770) * screenWidth;
CGFloat lowerY = (403 / 370) * screenHeight;
self.button.frame = CGRectMake(firstX, lowerY, width, height);
CGFloat secondX = (124 / 424) * screenWidth;
CGFloat upperY = (347 / 447) * screenHeight;
self.buttonTwo.frame = CGRectMake(secondX, upperY, width, height);
CGFloat thirdX = (232 / 680) * screenWidth;
self.buttonThree.frame = CGRectMake(thirdX, lowerY, width, height);
}];
}
【问题讨论】:
-
你想让按钮立即跳转到同一个起始矩形,然后动画到不同的目的地吗?你想改变它们的尺寸吗?发布的代码清楚地调整了按钮的大小,@DuncanC 的观察是关于浮点数学的。如果您希望按钮保持固定大小,则在它们的框架上使用 CGRectOffset,它只操纵原点。
标签: ios objective-c autolayout translate-animation