我是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);
}];
}
你想让按钮立即跳到相同的startingRect,然后动画到不同的目的地?你想让他们的尺码改变吗?发布的代码明确地调整了按钮的大小,并且@DuncanC的观察是关于浮点数学的。如果你想让这些按钮保持一个固定的大小,那么就在它们的框架上使用CGRectOffset,它们只能操纵起源。 – danh