2017-02-04 4 views
0

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); 
}]; 
} 
+1

버튼을 동일한 startRect로 즉시 이동 한 다음 다른 대상에 애니메이션을 적용 하시겠습니까? 크기가 전혀 바뀌 길 바래요? 코드를 게시하면 버튼 크기가 명확하게 조정되고 @ DuncanC의 관찰 결과는 부동 소수점 연산에 관한 것입니다. 버튼을 고정 된 크기로 유지하려면 프레임에서 CGRectOffset을 사용하면 원점을 조작 할 수 있습니다. – danh

답변

2

너의 높이와 너비가 나쁘다.

(42/40) * screenWidth는 (1) * screenWidth 또는 화면의 전체 너비로 단순화됩니다. (표현식 42/40은 정수 수학을 사용하여 수행되어 1.0이됩니다. 부동 소수점을 사용하는 경우, d가 1.05 * screenWidth가되면 이미지가 더 커집니다.)

높이 계산시 비슷한 문제가 있습니다. 버튼을 화면의 전체 높이로 설정하고 약간 넓게 설정합니다.

+0

예, 문제가있는 코드입니다. 나는 그것을 여러 번 변경했으나 버튼의 크기는 변경하지 않았습니다. 너비 : 78 높이 : 76. 무엇을 바꿀까요? 도와 주셔서 감사합니다! – Elizabeth429