我是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