本次要做的例子是iOS的彈出視圖動畫,不喜歡廢話,直接上圖:
上圖就是要實(shí)現(xiàn)的效果锌云,不喜歡看文章的人,點(diǎn)擊 這里 直達(dá)代碼。
該Demo中涉及兩個類:
PopOutView:彈出視圖類
MJPopTool:彈出視圖工具類
1涎永、PopOutView
.h文件代碼如下:
#import <UIKit/UIKit.h>
@class PopOutView;
typedef void(^PopOutViewDismissBlock)(PopOutView *popOutView);
@interface PopOutView : UIView
@property (nonatomic, copy) PopOutViewDismissBlock dismissBlock;
@end
.m代碼如下
#import "PopOutView.h"
@interface PopOutView ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *closeButton;
@end
@implementation PopOutView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_imageView = [[UIImageView alloc] init];
[self addSubview:_imageView];
_closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_closeButton setBackgroundImage:[UIImage imageNamed:@"red_packge_close"] forState:UIControlStateNormal];
[_closeButton addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_closeButton];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
_imageView.frame = self.bounds;
_imageView.image = [UIImage imageNamed:@"cat.jpg"];
_closeButton.frame = CGRectMake(self.bounds.size.width - 15, -15, 30, 30);
}
/**
* @author 王夢杰, 16-06-24 13:06:00
*
* 讓超出父視圖的子視圖響應(yīng)事件
*/
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
for (UIView *subView in self.subviews) {
CGPoint tp = [subView convertPoint:point fromView:self];
if (CGRectContainsPoint(subView.bounds, tp)) {
view = subView;
}
}
}
return view;
}
- (void)close {
if (_dismissBlock) {
_dismissBlock(self);
}
}
@end
這個界面直接放了一個imageView和closeButton,并且用block實(shí)現(xiàn)按鈕點(diǎn)擊事件的傳遞鹿响,這邊也可以使用代理羡微,看個人喜好。布局代碼比較簡單惶我,就不啰嗦了妈倔,這里有一段代碼可能需要講解一下用途:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
for (UIView *subView in self.subviews) {
CGPoint tp = [subView convertPoint:point fromView:self];
if (CGRectContainsPoint(subView.bounds, tp)) {
view = subView;
}
}
}
return view;
}
由于closeButton是PopOutView的子視圖,并且closeButton有一部分在PopOutView視圖的外面绸贡,會導(dǎo)致該部分的點(diǎn)擊無響應(yīng)盯蝴,上面那一段代碼的作用就是為了使超出父視圖邊界部分也可以得到響應(yīng)。如果想知道具體原理的恃轩,請看這里结洼。
2、MJPopTool
該類是一個單例類叉跛,提供了兩個對象方法松忍,一個用于彈出視圖,另一個用于關(guān)閉視圖筷厘。
/**
* 彈出視圖
*
* @param view 被彈出的視圖
* @param animated 是否需要動畫
*/
- (void)popView:(UIView *)view animated:(BOOL)animated;
/**
* 關(guān)閉視圖
*
* @param animated 是否需要動畫
*/
- (void)closeAnimated:(BOOL)animated;
這兩個方法的具體實(shí)現(xiàn)代碼:
- (void)popView:(UIView *)view animated:(BOOL)animated {
// 保存當(dāng)前彈出的視圖
_currentView = view;
CGFloat halfScreenWidth = [[UIScreen mainScreen] bounds].size.width * 0.5;
CGFloat halfScreenHeight = [[UIScreen mainScreen] bounds].size.height * 0.5;
// 屏幕中心
CGPoint screenCenter = CGPointMake(halfScreenWidth, halfScreenHeight);
view.center = screenCenter;
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[keyWindow addSubview:view];
if (animated) {
// 第一步:將view寬高縮至無限忻汀(點(diǎn))
view.transform = CGAffineTransformScale(CGAffineTransformIdentity,
CGFLOAT_MIN, CGFLOAT_MIN);
[UIView animateWithDuration:0.3
animations:^{
// 第二步: 以動畫的形式將view慢慢放大至原始大小的1.2倍
view.transform =
CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.2
animations:^{
// 第三步: 以動畫的形式將view恢復(fù)至原始大小
view.transform = CGAffineTransformIdentity;
}];
}];
}
}
- (void)closeAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:0.2
animations:^{
// 第一步: 以動畫的形式將view慢慢放大至原始大小的1.2倍
_currentView.transform =
CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.3
animations:^{
// 第二步: 以動畫的形式將view縮小至原來的1/1000分之1倍
_currentView.transform = CGAffineTransformScale(
CGAffineTransformIdentity, 0.001, 0.001);
}
completion:^(BOOL finished) {
// 第三步: 移除
[_currentView removeFromSuperview];
}];
}];
} else {
[_currentView removeFromSuperview];
}
}
文末再次附上源碼地址:去下載宏所。
順手點(diǎn)個星的都是好同志。