iOS - adding/removing a subview programmatically

后端 未结 2 1348
后悔当初
后悔当初 2020-12-30 06:00

Ok I want to add a UIImageView as a subview and then remove it after a couple of seconds in the way a splash screen works. I found three different approaches to do it but I

相关标签:
2条回答
  • 2020-12-30 06:11

    You could use an animation attached to the view's layer. Code below fades the view out - but there are many other ways to remove it. (you need to attach the QuartzCore framework)

    myImageView.layer.opacity = 0.0;
    // this is the state the view will be in after the animation (e.g. invisible)
    
    CABasicAnimation *theFade;
    
    theFade = [CABasicAnimation animationwithKeyPath:@"opacity"];
    theFade.duration = 10.0; 
    theFade.fromValue = [NSNumber numberWithFloat:1.0]; // i.e. visible
    theFade.toValue = [NSNumber numberWithFloat:0.0]; // i.e. invisible 
    [myImageView.layer addAnimation:theFade forKey:@"animateOpacity"]; 
    
    0 讨论(0)
  • 2020-12-30 06:16

    If you are not going to use the image again, there is no need to keep a pointer to it. Further, if you use IBOutlet, you need to add the view in IB as well. In this specific example I would say option 3 makes the most sence, especially considering that with this choice you can began with a standard "view based application" template and just add the bits about the image view and leave the rest alone. One last observation of choice 3 though; the 2 messages to window;

     [self.window addSubview:myViewController.view];
    
     [self.window makeKeyAndVisible];
    

    Appear to be outside the scope of any method. This is likely just a copy and paste error, but make note that they should located within "didFinishLaunchingWithOptions:"

    0 讨论(0)
提交回复
热议问题