addSubview动画

Mik*_*ike 57 iphone animation uiview

我有主要的UIView,我在那里显示不同的数据.然后我放了一个按钮,显示如下子视图:

- (IBAction) buttonClicked:(id)sender
{
    UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];
    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(25,25,50,25)];
    newLabel.text = @"some text";
    [newView addSubview:newLabel];

    [self.view addSubview:newView];
    [newLabel release];
    [newView release];
}
Run Code Online (Sandbox Code Playgroud)

newView看起来很好,但它不会以任何方式动画,它只会立即出现.当newView出现时,如何添加某种动画?像缩放或向下滑动或类似的东西.有一些简单的代码吗?

ade*_*doy 83

[UIView transitionWithView:containerView duration:0.5
        options:UIViewAnimationOptionTransitionCurlUp //change to whatever animation you like
        animations:^ { [containerView addSubview:subview]; }
        completion:nil];
Run Code Online (Sandbox Code Playgroud)


Ric*_*kiG 79

嗨您可以使用UIView动画:

[newView setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)]; //notice this is OFF screen!
[UIView beginAnimations:@"animateTableView" context:nil];
[UIView setAnimationDuration:0.4];
[newView setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; //notice this is ON screen!
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

SDK现在将为您解决这个问题,并将视图设置为您给出的位置的动画.这适用于UIViews的大多数属性:alpha,scale,bounds,frames等.

还有动画作为翻转和卷曲:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                           forView:newView
                            cache:YES];

[self.navigationController.view addSubview:settingsView.view];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

希望这有助于让你开始:)


Cha*_*aka 25

链接QuarzCore框架

#import <QuartzCore/QuartzCore.h>

CATransition *transition = [CATransition animation];
transition.duration = 1.0;
transition.type = kCATransitionPush; //choose your animation
[newView.layer addAnimation:transition forKey:nil];
[self.view addSubview:newView]; 
Run Code Online (Sandbox Code Playgroud)

  • +1您还可以使用过渡子类型来进一步自定义过渡。https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CATransition_class/#//apple_ref/doc/constant_group/Common_Transition_Subtypes (2认同)

Kyl*_*egg 21

我发现即使使用动画块和选项,尝试addSubview单独制作动画也不会对我有任何帮助.我找到了一种解决方法,即将子视图的alpha设置为0.0,添加子视图,然后将alpha的设置从0.0设置为1.0.这给了我正在寻找的淡出效果.

希望这有助于其他人.

UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
redView.backgroundColor = [UIColor redColor];
redView.alpha = 0.0;

[self.view addSubview:redView];

[UIView animateWithDuration:0.5 animations:^{
  redView.alpha = 1.0;
}];
Run Code Online (Sandbox Code Playgroud)


Rob*_*ack 21

让我们快速做到这一点.

var redView = UIView(frame:CGRectMake(20,20,100,100))
redView.backgroundColor = UIColor.redColor
redView.alpha = 0.0
view.addSubview(redView)

UIView.animateWithDuration(0.25) { () -> Void in
    redView.alpha = 1.0
}
Run Code Online (Sandbox Code Playgroud)

只需将其添加到animateWithDuration块中,就无法动画添加子视图.它不能使用隐藏动画.


小智 7

雨燕5

UIView.transition(with: renderView, duration: 0.25, options: .transitionCrossDissolve, animations: {
        self.renderView.addSubview(emojiView)
        self.renderView.bringSubviewToFront(emojiView)
    }, completion: nil)
Run Code Online (Sandbox Code Playgroud)

在代码示例中,renderView 是您将添加子视图的视图