简体   繁体   English

如何将UIView隐藏和显示(动画)为UIActionSheet?

[英]How can i hide and display (animation) of UIView as UIActionSheet?

I am new at iOS development, So please attention at My Question. 我是iOS开发的新手,请关注我的问题。

As my title said, 正如我的标题所说,

How Can i hide and display UIView ( with Animation ) as UIActionSheet ? 我如何隐藏和显示UIView带动画 )作为UIActionSheet
Searched in google, bud didn't find a valid solution.. 在谷歌搜索,芽没有找到有效的解决方案..

NOTE : i found following line but its not help me. 注意: 我发现以下行,但它没有帮助我。

Try following code: 请尝试以下代码:

- (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);
    }];
}

Well in this case what you can do is add a UIView on the window and then show it when you want to. 那么在这种情况下你可以做的是在窗口上添加一个UIView ,然后在你想要的时候显示它。 So, firstly what you will have to do is create an object of AppDelegate . 所以,首先你要做的是创建一个AppDelegate的对象。

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

And create your view like 并创建你的视图

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

This is to show your view on the window, note the y coordinate of your view should be below the dimensions of the screen so that you can bring it back up to a certain level and then send it down again. 这是为了在窗口上显示您的视图,请注意您的视图的y坐标应低于屏幕的尺寸,以便您可以将其恢复到某个级别,然后再将其向下发送。

[appDelegate.window addSubview:ActionView];

Then simply add these custom animations to show and hide your view 然后,只需添加这些自定义动画即可显示和隐藏您的视图

To Reveal your View 揭示你的观点

[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];  

To Hide your View 隐藏您的视图

[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];

Lastly you can adjust to your own dimensions and frames. 最后,您可以根据自己的尺寸和框架进行调整。 Hope this helps... 希望这可以帮助...

Try this solution.... it works 试试这个解决方案......它有效

#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];
}

For achieving this, follow the below steps:- 为实现此目的,请按照以下步骤操作: -

1) Drag UIView named as pickerView, add inside your view. 1)拖动名为pickerView的UIView ,在视图中添加。

2) Drag UIToolBar (if needed), add inside your view. 2)拖动UIToolBar (如果需要),在视图中添加。

3) Drag UIPickerView , add inside your view. 3)拖动UIPickerView ,在视图中添加。

4) Connect the IBOutlet to pickerView. 4)将IBOutlet连接到pickerView。

5) Now implement the below methods inside your code. 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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM