1.遮罩制作
重载layoutSubviews函数,要了解这个函数就看看UIView生命周期
_bottomHeight :下面弹出框的高度
CGAffineTransformMakeTranslation 定义运动方向负_bottomHeight表示0.3秒内移动这个距离
弹出结束后,即completion 后执行后面的内容
//在这个类里重载
@interface myActionSheet : UIView
@end
- (void)layoutSubviews {
[super layoutSubviews];
if (_bottomHeight > 0) {
[UIView animateWithDuration:0.3 animations:^{
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
self.bottomView.transform = CGAffineTransformMakeTranslation(0, -_bottomHeight);
} completion:^(BOOL finished) {
//自己想显示的内容
}];
_bottomHeight = 0;
}
}
2.当遮罩住屏幕后,点一下立马消失
#pragma mark - Override Method
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self dismiss];
}
- (void)dismiss {
[UIView animateWithDuration:0.3 animations:^{
self.backgroundColor = [UIColor clearColor];
self.bottomView.transform = CGAffineTransformMakeTranslation(0,CGRectGetHeight(self.bottomView.frame));
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
另外一种制造延迟的方法
self.collectionView.transform = CGAffineTransformMakeTranslation(0.01, self.HeightFrame);
[myGCDQueue executeInMainQueue:^{
[UIView animateWithDuration:0.3 animations:^{
self.collectionView.transform = CGAffineTransformMakeTranslation(0, 0);
self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
}completion:^(BOOL finished) {
}];
}afterDelaySecs:0.1];
@interface myGCDQueue : NSObject
//主线程延时执行gcd
+ (void)executeInMainQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
@end
@implementation myGCDQueue
+ (void)executeInMainQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec {
NSParameterAssert(block);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * sec),dispatch_get_main_queue(), block);
}
@end
本文介绍在iOS中如何使用UIView的layoutSubviews方法和Core Graphics进行遮罩视图的弹出与消失动画效果实现,包括动画延迟执行的两种方法。
5196

被折叠的 条评论
为什么被折叠?



