iOS - 金币奖励弹框

实现效果如图
WX20170725-161326@2x.png
封装工具MyPopTool.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface MyPopTool : NSObject
+ (instancetype)sharedInstance;

- (void)popView:(UIView *)view animated:(BOOL)animated;

- (void)closeAnimated:(BOOL)animated;
@end
MyPopTool.m
#import "MyPopTool.h"

@interface MyPopTool ()
@property (nonatomic, strong) UIView *currentView;
@end

@implementation MyPopTool
+ (instancetype)sharedInstance {
    static MyPopTool *_popTool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _popTool = [[MyPopTool alloc] init];
    });
    return _popTool;
}

- (void)popView:(UIView *)view animated:(BOOL)animated {
    _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宽高缩至无限小(点)
        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恢复至原始大小
                view.transform = CGAffineTransformIdentity;
            }];
        }];
    }
}

- (void)closeAnimated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:0.2 animations:^{
            _currentView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                _currentView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
            } completion:^(BOOL finished) {
                [_currentView removeFromSuperview];
            }];
        }];
    } else {
        [_currentView removeFromSuperview];
    }
}
具体用法如下:
  • 懒加载,要弹出视图的样式
- (UIView *)popOutView{
    if (!_popOutView) {
        _popOutView = [[UIView alloc] initWithFrame:self.view.bounds];
        _popOutView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.400];
        _popOutView.alpha = 0.9f;
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH_NO_STATUS * 0.5 - 75, SCREEN_HEIGHT_NO_STATUS * 0.5 - 100, 150, 200)];
        bgView.layer.masksToBounds = YES;
        bgView.layer.cornerRadius = 5.0 ;
        bgView.backgroundColor = [UIColor whiteColor];
        [_popOutView addSubview:bgView];
        
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0, 150, 150)];
        img.image = [UIImage imageNamed:@"ubit_sucess"];
        [bgView addSubview:img];
        
        UILabel *prompt = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 150, 50)];
        prompt.numberOfLines = 0;
        prompt.textColor = [UIColor orangeColor];
        prompt.textAlignment = NSTextAlignmentCenter;
        self.promptLabel = prompt;
        [bgView addSubview:prompt];
    }
    return _popOutView;
}
  • 打开视图
[[MyPopTool sharedInstance] popView:self.popOutView animated:YES];
  • 关闭视图
[[MyPopTool sharedInstance] closeAnimated:NO];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文链接http://www.cnblogs.com/kenshincui/p/4186022.html 音频在i...
    Hyman0819阅读 21,809评论 4 74
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,250评论 4 61
  • ...... 那是希望的春天,那是失望的冬天; 我们全部都在直奔天堂,我们全部都在直奔相反的方向。 ...
    幸运石阅读 135评论 0 0
  • 有人认为,只有经过繁琐的工艺,一道又一道复杂的工序,所提炼出来的艺术品,才足够完美,才能堪称为美;也有人认为...
    青挽997阅读 355评论 0 1
  • 在天空之下,山丘之上。 是寂静的夜晚,皎洁月光。 树被风摇曳作响,星星明亮。 萤火之森闪烁微光,舞会才刚刚开始,夜...
    莫西仍然阅读 767评论 0 4