【问题标题】:How to show UIView like UIAction sheet using UIView animations in iOS?如何在 iOS 中使用 UIView 动画显示 UIView,如 UIAction 表?
【发布时间】:2015-12-21 06:52:13
【问题描述】:

嗨,我是 iOS 新手,我在 MainViewController 的底部添加了一个 UIView

当我点击一个按钮时,UIView 使用UIView 动画显示为“UIAction sheet”。

为此,我尝试了下面的代码,但是当我点击按钮时动画不适用。

请帮助我。

我的代码:-

#import "AnimationUIview.h"

@interface AnimationUIview ()
{
    UIView * firstView;
    NSLayoutConstraint * Bottom;
}

@end

@implementation AnimationUIview

- (void)viewDidLoad {
    [super viewDidLoad];

    firstView = [[UIView alloc] init];
    firstView.backgroundColor = [UIColor lightGrayColor];
    firstView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:firstView];

    NSDictionary * views = NSDictionaryOfVariableBindings(firstView);

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[firstView]-0-|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[firstView(100)]" options:0 metrics:nil views:views]];

    Bottom = [NSLayoutConstraint constraintWithItem:firstView
                                       attribute:NSLayoutAttributeBottom
                                       relatedBy:NSLayoutRelationEqual
                                          toItem:self.view
                                       attribute:NSLayoutAttributeBottom
                                      multiplier:1
                                        constant:80];
    [self.view addConstraint:Bottom];
}

- (IBAction)buttonAction:(id)sender {

    [UIView animateWithDuration:0.5f
                          delay:0.0f
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{

                         Bottom.constant = -10;
                     }
                     completion:nil];
}

@end

【问题讨论】:

    标签: ios iphone animation uiview uiactionsheet


    【解决方案1】:
    #import "AnimationUIview.h"
    
    @interface AnimationUIview () {
        UIView * firstView;
        NSLayoutConstraint * Bottom;
        BOOL isshown;
    }
    
    @end
    @implementation AnimationUIview
    -(void)viewDidLoad {
        [super viewDidLoad];
    
        firstView = [[UIView alloc] init];
        firstView.backgroundColor = [UIColor lightGrayColor];
        firstView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:firstView];
    
        NSDictionary * views = NSDictionaryOfVariableBindings(firstView);
    
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[firstView]-0-|" options:0 metrics:nil views:views]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[firstView(100)]" options:0 metrics:nil views:views]];
    
        Bottom = [
                 NSLayoutConstraint
                 constraintWithItem:firstView
                 attribute:NSLayoutAttributeBottom
                 relatedBy:NSLayoutRelationEqual
                 toItem:self.view
                 attribute:NSLayoutAttributeBottom
                 multiplier:1
                 constant:80
        ];
        [self.view addConstraint:Bottom];
        isshown=No;
    
    }
    
    - (IBAction)buttonAction:(id)sender {
    
        if (isshown==NO) {
            firstview.frame =  CGRectMake(0,0, SCREEN_WIDTH, 100);
            [UIView animateWithDuration:0.40 animations:^{
                firstview.frame =  CGRectMake(0, SCREEN_HEIGHT-100, SCREEN_WIDTH, 100);
                [firstview setAlpha:0.0f];
            } completion:^(BOOL finished) {
    
            }];
    
            isshow=Yes;
        } else {
            firstview.frame =  CGRectMake(0, SCREEN_HEIGHT-100, SCREEN_WIDTH, 100);
            [UIView animateWithDuration:0.40 animations:^{
                firstview.frame =  CGRectMake(0,0, SCREEN_WIDTH, 100);
                firstview.alpha = 1.0f;
            } completion:^(BOOL finished) {
            [firstview setHidden:YES];
        }];
    
        isshown=No;
    }
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      相关资源
      最近更新 更多