如何像UIActionSheet一样隐藏和显示(动画)UIView?

3
4个回答

7
尝试以下代码:

请执行以下代码:

- (IBAction)showTapped:(id)sender {
    hideButton.enabled=YES;
    showButton.enabled=NO;
    [UIView animateWithDuration:.5 animations:^{
        subView.frame=CGRectMake(0, 225, subView.frame.size.width, subView.frame.size.height);
    }];
}

- (IBAction)hideTapped:(id)sender {
    hideButton.enabled=NO;
    showButton.enabled=YES;
    [UIView animateWithDuration:.5 animations:^{
        subView.frame=CGRectMake(0, 480, subView.frame.size.width, subView.frame.size.height);
    }];
}

谢谢你的回答,朋友。它对我很有帮助 :) 简单、简洁而且正确 :) - user1525369

4
在这种情况下,您可以在窗口上添加一个 UIView,并在需要时显示它。因此,首先您需要做的是创建 AppDelegate 的对象。
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

并且创建您的视图如下:

 CGRect frame = CGRectMake(0, 460, 320, 300);
 UIView *ActionView = [UIView alloc]initWithFrame:frame];

这是显示窗口的视图,注意您的视图y坐标应低于屏幕尺寸,以便您可以将其提升到一定水平,然后再次发送它。

[appDelegate.window addSubview:ActionView];

然后,只需添加这些自定义动画以显示和隐藏您的视图。
显示您的视图:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.25];
CGRect rect = [ActionView frame];
rect.origin.y = -300;
[ActionView setFrame:rect];
[UIView commitAnimations];  

隐藏你的视图
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.25];
CGRect rect = [ScrollView frame];
rect.origin.y = 460;
[ScrollView setFrame:rect];
[UIView commitAnimations];

最后,您可以调整到自己的尺寸和框架。希望这能帮到您...

谢谢你的回答,完全正确。但是Shukhpal的回答可能比你简单 :) 我给你点赞了 +1 - user1525369
顺便提一下,他已经给你直接的代码了... 并没有提到视图被用作操作表。 - IronManGill
从iOS4+开始,苹果强烈推荐使用基于块的动画。 - holex

0

尝试这个解决方案...它有效

#pragma mark - Date Selector View PresentModelView with Transparent ViewController

- (void) showModal:(UIView*) modalView {

    UIWindow *mainWindow = [(AppDelegate *)[UIApplication sharedApplication].delegate window];

    CGPoint middleCenter;


    middleCenter = CGPointMake(modalView.center.x, modalView.center.y);

    CGSize offSize = [UIScreen mainScreen].bounds.size;

    CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, offSize.height * 1.5);
    modalView.center = offScreenCenter;

    if ([[mainWindow subviews] containsObject:modalView]) {
        [modalView removeFromSuperview];
    }


    [mainWindow addSubview:modalView];

    [mainWindow bringSubviewToFront:modalView];
    // Show it with a transition effect
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    // animation duration in seconds
    modalView.center = middleCenter;
    [UIView commitAnimations];

}

// Use this to slide the semi-modal view back down.
- (void) hideModal:(UIView*) modalView {

    CGSize offSize = [UIScreen mainScreen].bounds.size;
    CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, offSize.height * 1.5);
    [UIView beginAnimations:nil context:(__bridge void *)(modalView)];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(hideModalEnded:finished:context:)];
    modalView.center = offScreenCenter;
    [UIView commitAnimations];

}

- (void) hideModalEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

    UIView *modalView = (__bridge UIView *)context;
    [modalView removeFromSuperview];
}

0
为实现此目的,请按以下步骤操作:
1)拖动名为pickerView的UIView,添加到您的视图中。
2)拖动UIToolBar(如果需要),添加到您的视图中。
3)拖动UIPickerView,添加到您的视图中。
4)将IBOutlet连接到pickerView。
5)现在在您的代码中实现以下方法。
-(void)viewDidAppear:(BOOL)animated{
    self.pickerView.frame = CGRectMake(0, self.view.frame.size.height+300, self.view.frame.size.width, 300);
}

-(void)hidePickerView{
    self.isPickerHidden = YES;
    [UIView animateWithDuration:0.5 animations:^{
        self.pickerView.frame = CGRectMake(0, self.view.frame.size.height+300, self.view.frame.size.width, 300);
    }];
}

-(void)showPickerView{
    self.isPickerHidden = NO;
    [UIView animateWithDuration:0.5 animations:^{

        self.pickerView.frame = CGRectMake(0, self.view.frame.size.height-300, self.view.frame.size.width, 300);
    }];
}

//Assuming you are tapping on cell, or you can use button connecting to the IBAction as well  
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isPickerHidden){
      [self showPickerView];
    }
    else{
        [self hidePickerView];
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接