简体   繁体   中英

How to move image in background of view?

I have a problem in my application i have a moving image it works fine. But my image is also moving over a button, that i can't click when the image is before the button. How can i make sure that the image is moving on the background of my view so i can still press the button.

Also my image is it works fine, but when I tested the app the image go outside the view sometimes how can i prevent this?

This is the code for the moving image

- (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;
}
}

Add this code in viewDidLoad after [[self view] addSubview:image];

[self.view sendSubviewToBack:image]; //Send image to back

OR

[self.view bringSubviewToFront:button]; //Bring button to front

Hope it helps you..

将按钮视图置于最前面。

[self.view bringSubViewToFront:button.view];

Use any of these methods to manage view's hierarchy

[self.view insertSubView:atIndex:];
[self.view insertSubView:belowView:];
[self.view insertSubView:aboveView:];
[self.view bringSubViewToFont:];
[self.view sendSubviewToBack:];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM