您的位置:首页 > 移动开发 > IOS开发

IOS项目之弹出动画一

2015-06-17 15:25 459 查看
小区宝首页导航栏左边有一个物业按钮,点击时会出现一个视图动画,之前用的是一个POP第三方,想着几个POP动画就要引用一堆的第三方有点麻烦,就试着自己写了一下,功能实现了,下一步就是优化将其封装一下。下面我用DatePicker做的主要是想着再做出点击弹出按钮在底部出现DatePicker选择器。





#import "ViewController.h"
#import "PageViewController.h"
#import "myView.h"

@interface ViewController ()
@property(nonatomic,strong) myView *myview;
@property(nonatomic,strong) UIDatePicker *dataPicker;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"预览" style:UIBarButtonItemStyleDone target:self action:@selector(rightClick)];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"弹出" style:UIBarButtonItemStyleDone target:self action:@selector(leftClick)];

//遮罩层
_myview=[[myView alloc]initWithFrame:CGRectMake(0,-self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height)];
_myview.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.1f];
//为遮罩层添加手势识别 可以点击遮罩层空白处隐藏视图
UIGestureRecognizer *tapgesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapclick)];
_myview.userInteractionEnabled=YES;
[_myview addGestureRecognizer:tapgesture];

//设置DatePicker
_dataPicker=[[UIDatePicker alloc]init];
_dataPicker.frame=CGRectMake(0, 40, 0, 0);
//    datepicker.backgroundColor=[UIColor grayColor];
[_myview addSubview:_dataPicker];

//设置DatePicker上面的视图
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
view.backgroundColor=[UIColor blueColor];
UIButton *btnright=[UIButton buttonWithType:UIButtonTypeSystem];
[btnright setTitle:@"确定" forState:UIControlStateNormal];
[btnright addTarget:self action:@selector(btnrightClick:) forControlEvents:UIControlEventTouchUpInside];
btnright.frame=CGRectMake(self.view.bounds.size.width-40, 0, 40, 40);
[view addSubview:btnright];
[_myview addSubview:view];
[self.view addSubview:_myview];
}
-(void)leftClick
{
//下落动画 时间短一些
[UIView beginAnimations:@"text" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
_myview.frame=CGRectMake(0,40, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView commitAnimations];

//恢复动画 时间长一些
[UIView beginAnimations:@"text" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5];
_myview.frame=CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView commitAnimations];

}
-(void)tapclick
{
[UIView beginAnimations:@"text" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3];
_myview.frame=CGRectMake(0, -self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView commitAnimations];
}
-(void)btnrightClick:(id)sender
{
NSLog(@"%@",_dataPicker.date);
[self tapclick];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


我们稍加改动就可以变成从底部弹出视图 这样就完成了闪购模块 宝贝详情中的选择规格的功能

#import "ViewController.h"
#import "PageViewController.h"
#import "myView.h"

@interface ViewController ()
@property(nonatomic,strong) myView *myview;
@property(nonatomic,strong) UIDatePicker *dataPicker;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"预览" style:UIBarButtonItemStyleDone target:self action:@selector(rightClick)];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"弹出" style:UIBarButtonItemStyleDone target:self action:@selector(leftClick)];

//遮罩层
_myview=[[myView alloc]initWithFrame:CGRectMake(0,self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height)];
_myview.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.1f];
//为遮罩层添加手势识别 可以点击遮罩层空白处隐藏视图
UIGestureRecognizer *tapgesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapclick)];
_myview.userInteractionEnabled=YES;
[_myview addGestureRecognizer:tapgesture];

//设置DatePicker
_dataPicker=[[UIDatePicker alloc]init];
_dataPicker.frame=CGRectMake(0, self.view.bounds.size.height-300, 0, 0);
//    datepicker.backgroundColor=[UIColor grayColor];
[_myview addSubview:_dataPicker];

//设置DatePicker上面的视图
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-340, self.view.bounds.size.width, 40)];
view.backgroundColor=[UIColor blueColor];
UIButton *btnright=[UIButton buttonWithType:UIButtonTypeSystem];
[btnright setTitle:@"确定" forState:UIControlStateNormal];
[btnright addTarget:self action:@selector(btnrightClick:) forControlEvents:UIControlEventTouchUpInside];
btnright.frame=CGRectMake(self.view.bounds.size.width-40, 0, 40, 40);
[view addSubview:btnright];
[_myview addSubview:view];
[self.view addSubview:_myview];
}
-(void)leftClick
{
//下落动画 时间短一些
[UIView beginAnimations:@"text" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
_myview.frame=CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView commitAnimations];

//    //恢复动画 时间长一些
//    [UIView beginAnimations:@"text" context:nil];
//    [UIView setAnimationDelay:0];
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//    [UIView setAnimationDuration:0.5];
//    _myview.frame=CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height);
//    [UIView commitAnimations];

}
-(void)tapclick
{
[UIView beginAnimations:@"text" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3];
_myview.frame=CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView commitAnimations];
}
-(void)btnrightClick:(id)sender
{
NSLog(@"%@",_dataPicker.date);
[self tapclick];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end






上面的导航控制器并未被遮罩 ,这样添加时才能使导航控制器遮罩[[UIApplication sharedApplication].keyWindow addSubview:_myview];

//
//  ViewController.m
//  PhotoBrower
//
//  Created by City--Online on 15/6/16.
//  Copyright (c) 2015年 City--Online. All rights reserved.
//
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
#define SCREENWIDTH  [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height

#import "ViewController.h"
#import "PageViewController.h"
#import "ImageViewController.h"
#import "myView.h"

@interface ViewController ()
@property(nonatomic,strong) myView *myview;
@property(nonatomic,strong) UIDatePicker *dataPicker;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"预览" style:UIBarButtonItemStyleDone target:self action:@selector(rightClick)];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"弹出" style:UIBarButtonItemStyleDone target:self action:@selector(leftClick)];

//遮罩层
_myview=[[myView alloc]initWithFrame:CGRectMake(0,SCREENHEIGHT, WIDTH, SCREENHEIGHT)];
_myview.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.1f];
//为遮罩层添加手势识别 可以点击遮罩层空白处隐藏视图
UIGestureRecognizer *tapgesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapclick)];
_myview.userInteractionEnabled=YES;
[_myview addGestureRecognizer:tapgesture];

//设置DatePicker
_dataPicker=[[UIDatePicker alloc]init];
_dataPicker.frame=CGRectMake(0, SCREENHEIGHT-220, 0, 0);
//    datepicker.backgroundColor=[UIColor grayColor];
[_myview addSubview:_dataPicker];

//设置DatePicker上面的视图
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, SCREENHEIGHT-260, WIDTH, 40)];
view.backgroundColor=[UIColor blueColor];
UIButton *btnright=[UIButton buttonWithType:UIButtonTypeSystem];
[btnright setTitle:@"确定" forState:UIControlStateNormal];
[btnright addTarget:self action:@selector(btnrightClick:) forControlEvents:UIControlEventTouchUpInside];
btnright.frame=CGRectMake(self.view.bounds.size.width-40, 0, 40, 40);
[view addSubview:btnright];
[_myview addSubview:view];
[[UIApplication sharedApplication].keyWindow  addSubview:_myview];
//    [self.view addSubview:_myview];
//    [[UIApplication sharedApplication].delegate.window addSubview:_myview];
}
-(void)leftClick
{
//下落动画 时间短一些
[UIView beginAnimations:@"text" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
_myview.frame=CGRectMake(0,0, self.view.bounds.size.width, SCREENHEIGHT);
[UIView commitAnimations];

//恢复动画 时间长一些
//    [UIView beginAnimations:@"text" context:nil];
//    [UIView setAnimationDelay:0];
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//    [UIView setAnimationDuration:0.5];
//    _myview.frame=CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height);
//    [UIView commitAnimations];

//    PageViewController *pageVc=[[PageViewController alloc]init];
//    pageVc.hidesBottomBarWhenPushed=YES;
////    [self presentViewController:pageVc animated:YES completion:nil];
//    [self.navigationController pushViewController:pageVc animated:YES];

}
-(void)tapclick
{
[UIView beginAnimations:@"text" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3];
_myview.frame=CGRectMake(0, SCREENHEIGHT, self.view.bounds.size.width, SCREENHEIGHT);
[UIView commitAnimations];
}
-(void)btnrightClick:(id)sender
{
NSLog(@"%@",_dataPicker.date);
[self tapclick];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: