IOS项目之弹出动画二

简介:

在IOS项目之弹出动画一中只是实现也功能,并没有体现面向对象的思想 ,今天就试着把它封装了一下,弹出视图的内容可以根据自定义,此处只是用UIDatePicker来演示

我把它传到了GitHub上   https://github.com/ywcui/YvanDatePicker.git

一、新建一个类YWDatePicker集成UIView


//  YvanDatePicker.h

#import <UIKit/UIKit.h>
typedef  void (^selectDate)(NSDate *date);

@interface YvanDatePicker : UIView

//单利
+ (YvanDatePicker *)sharedManager;

//block传值获取选择时间
@property(nonatomic,strong) selectDate selectDate;

//时间选择控件 可设置属性
@property(nonatomic,strong) UIDatePicker *datePicker;

//window全屏显示
-(void)showInWindow;

// View中显示
-(void)showInView:(UIView*)view;

//在父视图view的相对位置为Frame
-(void)showInView:(UIView*)view withFrame:(CGRect)frame;

//消失视图
-(void)dismissView;
@end


#define MAXHEIGHT [UIScreen mainScreen].bounds.size.height
#import "YvanDatePicker.h"

@interface YvanDatePicker ()

@end

@implementation YvanDatePicker


+ (YvanDatePicker *)sharedManager
{
    static YvanDatePicker *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
        sharedAccountManagerInstance.backgroundColor=[UIColor colorWithWhite:0.5 alpha:0.4];
    });
    return sharedAccountManagerInstance;
}

-(void)showInWindow
{
    [self showInView:[UIApplication sharedApplication].keyWindow];
}
-(void)showInView:(UIView*)view
{
    [self showInView:view withFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    
}
//frame相对于父视图的位置
-(void)showInView:(UIView*)view withFrame:(CGRect)frame;
{
    //在此可以自定义视图
    self.frame=CGRectMake(frame.origin.x, MAXHEIGHT, frame.size.width, frame.size.height);
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame=frame;
    } completion:nil];
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView)];
    [self addGestureRecognizer:tapGesture];
    if (_datePicker==nil) {
        _datePicker=[[UIDatePicker alloc]init];
        _datePicker.locale=[[NSLocale alloc ]initWithLocaleIdentifier:@"zh_Hans_CN"];
        _datePicker.datePickerMode=UIDatePickerModeDate;
        _datePicker.timeZone=[NSTimeZone defaultTimeZone];
    }
    _datePicker.frame=CGRectMake(0, frame.size.height-216, 0, 0);
    [self addSubview:_datePicker];
    
    [view addSubview:self];

}
-(void)dismissView
{
    _selectDate(_datePicker.date);
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame=CGRectMake(0, MAXHEIGHT, self.frame.size.width , self.frame.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

@end

二、调用


//
//  ViewController.m
//  YvanDatePicker
//
//  Created by City--Online on 15/6/18.
//  Copyright (c) 2015年 YvanCui. All rights reserved.
//

#import "ViewController.h"
#import "YvanDatePicker.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"弹出" style:UIBarButtonItemStyleDone target:self action:@selector(leftClick)];
}
-(void)leftClick
{
    YvanDatePicker *picker=[YvanDatePicker sharedManager];
    picker.selectDate=^(NSDate *date)
    {
        NSLog(@"%@",date);
    };
//    //1.设置在父视图的Frame
//    CGRect frame=CGRectMake(10, self.view.bounds.size.height-260, self.view.bounds.size.width-20, 260);
//    [picker showInView:self.view withFrame:frame];
//    
//    //2.Window显示
//    [picker showInWindow];
//    
//    //3.View全屏显示
//    [picker showInView:self.view];
    
    //4.相对于Window的Frame
    CGRect frame1=CGRectMake(0, [UIApplication sharedApplication].keyWindow.bounds.size.height-260, [UIApplication sharedApplication].keyWindow.bounds.size.width, 260);
    [picker showInView:[UIApplication sharedApplication].keyWindow withFrame:frame1];
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

三、显示效果

这个还可以进一步优化可以加一个标记值可以防止连续点击时一直弹出


#define MAXHEIGHT [UIScreen mainScreen].bounds.size.height
#import "YvanDatePicker.h"

@interface YvanDatePicker ()
@property(nonatomic,assign) BOOL openFlag;
@end

@implementation YvanDatePicker


+ (YvanDatePicker *)sharedManager
{
    static YvanDatePicker *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
        sharedAccountManagerInstance.backgroundColor=[UIColor colorWithWhite:0.5 alpha:0.4];
    });
    return sharedAccountManagerInstance;
}

-(void)showInWindow
{
    [self showInView:[UIApplication sharedApplication].keyWindow];
}
-(void)showInView:(UIView*)view
{
    [self showInView:view withFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    
}
//frame相对于父视图的位置
-(void)showInView:(UIView*)view withFrame:(CGRect)frame;
{
    if (_openFlag) {
        [self dismissView];
        return;
    }
    
    _openFlag=true;
    self.frame=CGRectMake(frame.origin.x, -frame.size.height, frame.size.width, frame.size.height);
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame=CGRectMake(frame.origin.x, 104, frame.size.width, frame.size.height);;
    } completion:nil];
    [UIView animateWithDuration:0.3 delay:0.4 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame=CGRectMake(frame.origin.x, 0, frame.size.width, frame.size.height);;
    } completion:nil];
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView)];
    [self addGestureRecognizer:tapGesture];
    if (_datePicker==nil) {
        _datePicker=[[UIDatePicker alloc]init];
        _datePicker.locale=[[NSLocale alloc ]initWithLocaleIdentifier:@"zh_Hans_CN"];
        _datePicker.datePickerMode=UIDatePickerModeDate;
        _datePicker.timeZone=[NSTimeZone defaultTimeZone];
    }
    _datePicker.frame=CGRectMake(0, frame.size.height-216, 0, 0);
    [self addSubview:_datePicker];
    
    [view addSubview:self];

}
-(void)dismissView
{
    _openFlag=false;
    _selectDate(_datePicker.date);
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame=CGRectMake(0,- self.frame.size.height, self.frame.size.width , self.frame.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

@end


YvanDatePicker *picker=[YvanDatePicker sharedManager];
    picker.selectDate=^(NSDate *date)
    {
        NSLog(@"%@",date);
    };
//    //1.设置在父视图的Frame
    CGRect frame=CGRectMake(0, 0, self.view.bounds.size.width, 260);
    [picker showInView:self.view withFrame:frame];

回弹效果

有了这个东西,妈妈再也不用担心我的学习,下面的这几个都可以做


相关文章
|
7月前
|
iOS开发
iOS 动画绘制圆形
iOS 动画绘制圆形
42 1
|
3月前
|
iOS开发
iOS App Store 上传项目报错 缺少隐私政策网址 (URL) 解决方法
iOS App Store 上传项目报错 缺少隐私政策网址 (URL) 解决方法
iOS App Store 上传项目报错 缺少隐私政策网址 (URL) 解决方法
|
9月前
|
缓存 移动开发 前端开发
iOS项目组件化历程
随着业务的发展,App中的页面,网络请求,通用弹层UI,通用TableCell数量就会剧增,需求的开发人员数量也会逐渐增多。 如果所有业务都在同一个App中,并且同时开发人数较少时,抛开代码健壮性不谈,实际的开发体验可能并没有那么糟糕,毕竟作为一个开发,什么地方用什么控件,就跟在HashMap中通过Key获取Value那么简单。 那么当业务成长到需要分化到多个App的时候,组件化的重要性开始体现了。
55 0
|
9月前
|
iOS开发 Perl
将Flutter引入到现有项目中(iOS+Flutter)
将Flutter引入到现有项目中(iOS+Flutter)
|
10月前
|
JavaScript iOS开发
iOS上架之HBuider打包简单项目及注意事项
iOS上架之HBuider打包简单项目及注意事项
|
11月前
|
开发工具 iOS开发
iOS 项目无法在模拟器运行解决办法
iOS 项目无法在模拟器运行解决办法
448 0
|
11月前
|
JavaScript iOS开发
iOS上架之hubuilder打包Vue项目
1.官网下载最新的HBuilderx。 2.准备好一个包含manifest.json的Vue项目,打开进行详细设置,设置完成后,点击保存。 3.首先去App Uploader找到开发证书(开发类型是带development的),点击证书P12下载到桌面。 4.然后根据里面的Bundle ID新建一个描述文件,把描述文件也下载到桌面。 5.菜单项点击‘发行->云打包-打原生安装包’,出现如下图所示弹框,取消广告勾选项,点击打包,把前面下载好的证书,描述文件上传到原生打包列表,提交打包,等待打包完成。(仅以iOS为例) 6.打包完成后,点击“OK”保存安装包到目标文件,发送到手机端安装即可运行。
|
存储 编解码 编译器
iOS项目Project 和 Targets配置详解
最近开始学习完整iOS项目的开发流程和思路,在实际的项目开发过程中,我们通常需要对项目代码和资料进行版本控制和管理,一般比较常用的SVN或者Github进行代码版本控制和项目管理。
691 0
|
JavaScript iOS开发
ios上架之​HBuider打包简单项目及注意事项
ios上架之​HBuider打包简单项目及注意事项
70 0
ios上架之​HBuider打包简单项目及注意事项
|
iOS开发 Perl
iOS开发 -多Target项目如何优雅的使用pods
iOS开发 -多Target项目如何优雅的使用pods
377 0