如何在 iOS 中使用 UIView 动画显示 UIView,如 UIAction 表?
Posted
技术标签:
【中文标题】如何在 iOS 中使用 UIView 动画显示 UIView,如 UIAction 表?【英文标题】:How to show UIView like UIAction sheet using UIView animations in iOS? 【发布时间】: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
【问题讨论】:
【参考方案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
【讨论】:
以上是关于如何在 iOS 中使用 UIView 动画显示 UIView,如 UIAction 表?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用动画概念将一个 UIview 推送到 ios 中的另一个 UIview?
iOS Playgrounds 是不是支持 UIView 动画?