如何在iPhone中隐藏/显示带动画的UIView

时间:2013-08-23 05:48:05

标签: ios objective-c uiview uiviewanimation

我希望在用动画按下按钮后显示UIView,我可以显示视图,但我无法再按下该按钮隐藏它。这是我的代码显示/隐藏视图。 展示UIView:

    sliderView.frame =  CGRectMake(130, 20, 0, 0);
    [UIView animateWithDuration:0.25 animations:^{
    sliderView.frame =  CGRectMake(130, 30, 100, 200);
    }]; 

隐藏UIView:

     [UIView animateWithDuration:0.25 animations:^{
     sliderView.frame =  CGRectMake(130, 30, 0, 0);
    }]; 

使用上面的代码视图显示动画但不隐藏。 有人知道如何隐藏它,请帮助,谢谢

6 个答案:

答案 0 :(得分:18)

您的代码有效,我已经使用过了。看下面的代码
.h文件:

#import <UIKit/UIKit.h>

@interface StackoverflowTestViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *cautionView;


- (IBAction)btnToggleClick:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *btnToggle;
@end


.m文件:

#import "StackoverflowTestViewController.h"

@interface StackoverflowTestViewController ()

@end

@implementation StackoverflowTestViewController

bool isShown = false;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnToggleClick:(id)sender {
    if (!isShown) {
        _cautionView.frame =  CGRectMake(130, 20, 0, 0);
        [UIView animateWithDuration:0.25 animations:^{
            _cautionView.frame =  CGRectMake(130, 30, 100, 200);
        }];
        isShown = true;
    } else {
        [UIView animateWithDuration:0.25 animations:^{
            _cautionView.frame =  CGRectMake(130, 30, 0, 0);
        }];
        isShown = false;
    }
}
@end

答案 1 :(得分:6)

  -(void)fadeInAnimation:(UIView *)aView {

    CATransition *transition = [CATransition animation];
    transition.type =kCATransitionFade;
    transition.duration = 0.5f;
    transition.delegate = self;
    [aView.layer addAnimation:transition forKey:nil];
    }

Add Subview:

     [self.view addsubView:mysubview]; 
     [self fadeInAnimation:self.view];

Remove SubView:

      [mySubView removefromSuperView];   
      [self fadeInAnimation:self.view];

答案 2 :(得分:4)

您可以使用UIView的alpha属性和隐藏属性来隐藏您的sliderView。请尝试以下代码:

/*To unhide*/
[sliderView setHidden:NO];
sliderView.frame =  CGRectMake(130, 20, 0, 0);
[UIView animateWithDuration:0.25 animations:^{
    sliderView.frame =  CGRectMake(130, 30, 100, 200);
    sliderView.alpha = 1.0f;
} completion:^(BOOL finished) {
}];

/*To hide*/
[UIView animateWithDuration:0.25 animations:^{
    sliderView.frame =  CGRectMake(130, 30, 0, 0);
    [sliderView setAlpha:0.0f];
} completion:^(BOOL finished) {
    [sliderView setHidden:YES];
}];

答案 3 :(得分:2)

试试这个

- (IBAction)eventparameter:(id)sender
{
    if ([self.parameterview isHidden])
    {
        [UIView beginAnimations:@"LeftFlip" context:nil];
        [UIView setAnimationDuration:0.8];
        _parameterview.hidden=NO;
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown    forView:_parameterview cache:YES];
        [UIView commitAnimations];   
    }
    else
    {
        [UIView transitionWithView:_parameterview
                          duration:0.4
                           options:UIViewAnimationOptionTransitionCurlUp
                        animations:NULL
                        completion:NULL];

        [self.parameterview  setHidden:YES]; 
    }
}

答案 4 :(得分:0)

应用您的初始/起始帧视图

sliderView.frame =  CGRectMake(130, 480, 100, 200);

为您的按钮添加标签,例如

myButton.tag = 101;

你的按钮方法

-(void)MyButonMethod:(UIButton *)sender
{
  if(sender.tag == 101)
  {
    [UIView animateWithDuration:0.25 animations:^{
         sliderView.frame =  CGRectMake(130, 30, 100, 200);
    }]; 

    sender.tag = 102;
  }
  else
  {
    [UIView animateWithDuration:0.25 animations:^{
         sliderView.frame =  CGRectMake(130, 480, 100, 200);
    }];     
    sender.tag = 101;  
  }
}

答案 5 :(得分:0)

试试这个:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.25];
[sliderView setFrame:CGRectMake(130, 30, 0, 0)];
[UIView commitAnimations];