[英]How to move image in background of view?
我的應用程序中有一個問題,我有一個運動圖像,它工作正常。 但是我的圖像也在按鈕上移動,當圖像在按鈕之前時,我無法單擊。 我如何確保圖像在視圖的背景上移動,所以我仍然可以按按鈕。
我的圖像也可以正常工作,但是當我測試該應用程序時,圖像有時會超出視圖范圍,我該如何防止呢?
這是運動圖像的代碼
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
[self loadImage];
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cow.png"]];
image.frame =self.view.bounds;
[[self view] addSubview:image];
[NSTimer scheduledTimerWithTimeInterval: 3
target: self
selector:@selector(moveImage:)
userInfo: nil repeats:YES];
}
-(void) moveImage: (NSTimer*)timer {
CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);
CGFloat y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height);
CGPoint pointOne=CGPointMake(x,y);
image.center=pointOne;
}
}
在[[self view] addSubview:image];
之后,在viewDidLoad
添加此代碼[[self view] addSubview:image];
[self.view sendSubviewToBack:image]; //Send image to back
要么
[self.view bringSubviewToFront:button]; //Bring button to front
希望對您有幫助。
將按鈕視圖置於最前面。
[self.view bringSubViewToFront:button.view];
使用以下任何一種方法來管理視圖的層次結構
[self.view insertSubView:atIndex:]; [self.view insertSubView:belowView:]; [self.view insertSubView:aboveView:]; [self.view bringSubViewToFont:]; [self.view sendSubviewToBack:];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.