2014-04-02 2 views
0

이미지를 터치하여 임의의 지점으로 이동하려고 시도하고 있지만 이제는 설정 공간에 머물러 있어야한다는 것을 알게되었습니다. 그래서 점수 카운터 같은 것을 추가 할 수 있습니다. 내가 겪고있는 문제는 어떤 도움을받을 수 있을지 모르겠다.이미지가 설정된 영역 밖으로 나가는 것을 방지하는 방법

내 코드 :

-(IBAction)hideImage { 

CGFloat xRange = self.view.bounds.size.width - Image.bounds.size.width; 
CGFloat yRange = self.view.bounds.size.height - Image.bounds.size.height; 

CGFloat minX = self.view.center.x - (xRange/2); 
CGFloat minY = self.view.center.y - (yRange/2); 

int randomX = (arc4random() % (int)floorf(xRange)) + minX; 
int randomY = (arc4random() % (int)floorf(yRange)) + minY; 
Image.center = CGPointMake(randomX, randomY); 

}

감사합니다.

답변

0

원하는 프레임이있는 UIView를 사용할 수 있습니다. 그런 다음에 UIView에있는 UIImageView를 추가하고 설정 : 그래서

[thisView setClipsToBounds: YES]; 

:

CGRect myBounds = CGRectMake(0, 0, 320, 300); 
UIView* thisView = [[UIView alloc] initWithFrame: myBounds]; 
[self.view addSubview: thisView]; 

UIImageView* image = [[UIImageView alloc] initWithFrame:...]; 
//Other image code 
[thisView addSubview: image]; 

[thisView setClipsToBounds: YES]; 
0

을 당신이 서브 뷰으로 movingImageView라는 UIImageView에 속성이있는 경우,이 시도 :

-(IBAction)hideImage { 
    UIView *containerView = self.view; 
    UIView *movingView = self.movingImageView; 
    CGFloat xRange = CGRectGetWidth(containerView.bounds) - CGRectGetWidth(movingImageView.bounds); 
    CGFloat yRange = CGRectGetHeight(containerView.bounds) - CGRectGetHeight(movingImageView.bounds); 

    CGFloat minX = CGRectGetMidX(movingImageView.bounds); 
    CGFloat minY = CGRectGetMidY(movingImageView.bounds); 

    NSInteger randomX = (arc4random() % (NSInteger)floorf(xRange)) + minX; 
    NSInteger randomY = (arc4random() % (NSInteger)floorf(yRange)) + minY; 
    self.movingImageView.center = CGPointMake(randomX, randomY); 
} 

imageView의 경계는 컨테이너의 경계보다 크거나 같습니다. '이미지'가 실제로있는 UIButton에 포함되는 경우

이 이미 가지고 같이 거의 동일한 코드입니다, 당신은 할 메소드 선언을 변경할 수 있습니다 :

-(IBAction)hideImage:(id)sender { 
    UIView *containerView = self.view; 
    UIView *movingView = (UIView *)sender 
    ... The rest is same as above ... 
관련 문제