【问题标题】:load UIView from left animation on UIButton click从 UIButton 点击​​左侧动画加载 UIView
【发布时间】:2014-12-10 07:01:56
【问题描述】:

我在导航栏上有一个 UIButton 作为右栏按钮项。 我想在单击 UIButton 时显示一个视图, 并且该视图应该从右到左显示动画并停止为用户执行一些操作。 当用户完成视图上的工作时,他将单击相同的按钮,视图将从右到左隐藏动画。

我尝试正常加载视图我成功了, 当我应用动画时 视图从右到左动画并消失。 我是这个动画场景的新手。 所以任何人都可以指导我动画视图并在同一个按钮上显示隐藏操作。

这是我尝试过的代码,

-(IBAction)showView :(id)sender
{
    CGRect screen = [[UIScreen mainScreen]applicationFrame];
    UIView *quizInfoViewObj;
    quizInfoViewObj = [[UIView alloc]init];
    quizInfoViewObj.frame = CGRectMake(70, 0, 250, 480);
    quizInfoViewObj.backgroundColor = [UIColor orangeColor];
    [UIView beginAnimations: nil context: NULL];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDidStopSelector: @selector(pushAnimationDidStop:finished:context:)];
    quizInfoViewObj.center = self.view.center;
    quizInfoViewObj.center = CGPointMake(self.view.center.x - CGRectGetWidth(self.view.frame), self.view.center.y);
    [UIView commitAnimations];

    [self.view addSubview:quizInfoViewObj];
}

这里是我想要的...

【问题讨论】:

    标签: ios xcode uiviewanimation


    【解决方案1】:

    此代码可能会对您有所帮助。

       -(IBAction)showView :(id)sender
        {
            UIButton *button = (UIButton *)sender;
    
            if ([button isSelected] == NO) {
    
            CGRect screen = [[UIScreen mainScreen]applicationFrame];
            // UIView *quizInfoViewObj; // use this line in .h file variables declaration
    
            quizInfoViewObj = [[UIView alloc]init];
            quizInfoViewObj.frame = CGRectMake(self.view.frame.width, 0, 250, 480);
    
            quizInfoViewObj.backgroundColor = [UIColor orangeColor];
    
            [self.view addSubview:quizInfoViewObj];
    
            [UIView beginAnimations: nil context: NULL];
    
            quizInfoViewObj.frame = CGRectMake(70, 0, 250, 480);
    
            [UIView commitAnimations];
    
           }
           else {
    
            [UIView beginAnimations: nil context: NULL];
            [UIView setAnimationDelegate: self];
            [UIView setAnimationDidStopSelector: @selector(pushAnimationDidStop:finished:context:)];
    // In pushAnimationDidStop:finished:context method remove quizInfoViewObj from superview
    
            quizInfoViewObj.frame = CGRectMake(self.view.frame.width, 0, 250, 480);
    
            [UIView commitAnimations];
           }
        [button setSelected: ![button isSelected]];
    }
    

    【讨论】:

    • 您好,Mrunal,谢谢。有效。我在 pushAnimationDidStop 方法中隐藏视图谢谢
    • 在 if 块中,您要添加一个新的子视图,因此我建议您在 else 块中将其删除。否则每次它都会分配初始化一个新视图。或者在 viewDidLoad 中分配初始化这个视图并在 if-else 块中使用 setHidden 方法。
    • 好的,我将在加载主视图时分配一次视图,但视图的添加和删除将按照您的建议完成
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多