封装SlideQQ样式
来源:互联网 发布:linux多线程如何实现 编辑:程序博客网 时间:2024/06/06 00:54
// 最近模仿网上的一个demo,并且给其优化和封装了一下。 并打上了英文注释,秀一把英文。// ViewController.h#import <UIKit/UIKit.h>@class LeftViewController;@class RightViewController;@class MainViewController;@interface ViewController : UIViewController@property (nonatomic, strong) LeftViewController *leftVC;@property (nonatomic, strong) RightViewController *rightVC;@property (nonatomic, strong) MainViewController *mainVC;@property (nonatomic ,strong) UIImage *backgroundImage;// default is YES@property (nonatomic, assign, getter=isAnimted) BOOL animated;- (instancetype)initWithLeftVC:(LeftViewController *)leftVC rightVC:(RightViewController *)rightVC mainVC:(MainViewController *)mainVC backGroundImage:(UIImage *)backGroundImage;@end// ViewController.m#define Origin_Scale 1.0f#define Animation_Scale 0.9f#define Animation_Duration 0.3f#define Edge_Width Screen_Width*2/3#define CenterX_Offset 60.0f#define Screen_Width [[UIScreen mainScreen] bounds].size.width#define Screen_Height [[UIScreen mainScreen] bounds].size.height#define Screen_Frame [[UIScreen mainScreen] bounds]#import "ViewController.h"#import "RightViewController.h"#import "LeftViewController.h"#import "MainViewController.h"@interface ViewController ()@property (nonatomic, strong) UIImageView *mainBackgroundImageView;@end@implementation ViewController { // translated distance CGFloat _distance;}- (instancetype)initWithLeftVC:(LeftViewController *)leftVC rightVC:(RightViewController *)rightVC mainVC:(MainViewController *)mainVC backGroundImage:(UIImage *)backGroundImage { self = [super init]; if (self) { // default if (leftVC == nil) { self.leftVC = [[LeftViewController alloc] init]; } else if (rightVC == nil) { self.rightVC = [[RightViewController alloc] init]; } else if (mainVC == nil) { self.mainVC = [[MainViewController alloc] init]; } self.leftVC = leftVC; self.rightVC = rightVC; self.mainVC = mainVC; // imageView _mainBackgroundImageView = [[UIImageView alloc] initWithFrame:Screen_Frame]; if (backGroundImage != nil) { [_mainBackgroundImageView setImage:backGroundImage]; } else { _mainBackgroundImageView.backgroundColor = [UIColor clearColor]; } [self.mainVC.view addSubview:_mainBackgroundImageView]; // pan UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [self.mainVC.view addGestureRecognizer:panGest]; // tap UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]; [tapGest setNumberOfTapsRequired:1]; [self.mainVC.view addGestureRecognizer:tapGest]; [self.view addSubview:self.leftVC.view]; [self.view addSubview:self.rightVC.view]; [self.view addSubview:self.mainVC.view]; self.rightVC.view.hidden = YES; self.leftVC.view.hidden = YES; self.animated = YES; } return self;}#pragma mark - Gest Action- (void)tapAction:(UITapGestureRecognizer *)tapGest { // Show mainVC With animation if (tapGest.state == UIGestureRecognizerStateEnded) { [self showViewWithAnimationWithDuraion:Animation_Duration animatedScale:Origin_Scale centerX:Screen_Width/2]; }}- (void)panAction:(UIPanGestureRecognizer *)panGest { // translation In View CGPoint translation = [panGest translationInView:panGest.view]; // right slide CGFloat originX = panGest.view.frame.origin.x; _distance = [self distanceWithTranslationX:translation.x originX:originX]; panGest.view.center = CGPointMake(panGest.view.center.x + translation.x/2, panGest.view.center.y); CGFloat scale = 1-_distance/1000; panGest.view.transform = CGAffineTransformMakeScale(scale, scale); // return origin translation [panGest setTranslation:CGPointZero inView:panGest.view]; // hidden or not [self hiddenOrNotWithLeftView:self.leftVC.view rightView:self.rightVC.view originX:originX]; if (panGest.state == UIGestureRecognizerStateEnded ) { [self showViewWithOriginx:originX panGest:panGest]; }}#pragma mark - Show View// show view When gest' state become ended- (void)showViewWithOriginx:(CGFloat)originX panGest:(UIPanGestureRecognizer *)panGest { // show leftView With animation if (_distance > Edge_Width && originX > 0) { [self showViewWithAnimationWithDuraion:Animation_Duration animatedScale:Animation_Scale centerX:Screen_Width + CenterX_Offset]; } else if (_distance > Edge_Width && originX < 0) { // show rightView With animation [self showViewWithAnimationWithDuraion:Animation_Duration animatedScale:Animation_Scale centerX:-CenterX_Offset]; } else { // show mainView With animation [self showViewWithAnimationWithDuraion:Animation_Duration animatedScale:Origin_Scale centerX:Screen_Width/2]; }}// show view with animation- (void)showViewWithAnimationWithDuraion:(CGFloat)duraion animatedScale:(CGFloat)animatedScale centerX:(CGFloat)centerX { if (self.animated) { [UIView animateWithDuration:Animation_Duration animations:^{ self.mainVC.view.transform = CGAffineTransformMakeScale(animatedScale, animatedScale); self.mainVC.view.center = CGPointMake(centerX, Screen_Height/2.0f); }]; } else { self.mainVC.view.transform = CGAffineTransformMakeScale(animatedScale, animatedScale); self.mainVC.view.center = CGPointMake(centerX, Screen_Height/2.0f); } _distance = 0.0f;}#pragma mark - Config and Logic// update the distance- (CGFloat)distanceWithTranslationX:(CGFloat)translationX originX:(CGFloat)originX { if (originX <= 0) { _distance += (-1.0f) * translationX; } else { _distance += translationX; } return _distance;}// hidden or not- (void)hiddenOrNotWithLeftView:(UIView *)leftView rightView:(UIView *)rightView originX:(CGFloat)originX { if (originX <= 0) { leftView.hidden = YES; rightView.hidden = NO; } else { leftView.hidden = NO; rightView.hidden = YES; }}- (void)setBackgroundImage:(UIImage *)backgroundImage { _backgroundImage = backgroundImage; if (_backgroundImage != nil) { _mainBackgroundImageView.image = _backgroundImage; }}@end
0 0
- 封装SlideQQ样式
- 封装函数获取样式
- 弹出样式封装
- Silverlight 使用样式封装控件观感
- UITableView不同样式cell的封装
- Android 封装实现各种样式对话框
- 封装一个特别好看的进度条样式!
- 封装返回当前样式的函数
- 获取非行间样式(封装)
- android 关于封装tabbar,任意修改样式
- CSS常用 Less 公共样式封装
- 如何使用HTC文件来封装CSS样式
- 如何使用HTC文件来封装CSS样式
- 如何使用HTC文件来封装CSS样式
- (8):Silverlight 2 使用样式封装控件观感
- 第08课 使用样式封装控件观感
- 如何使用HTC文件来封装CSS样式
- 动画封装三:单条样式的缓动运动
- 9.13~9.30刷题(shui)题解
- HDU 5479(栈的应用)
- android学习-----Fragment
- axis webservice
- QCustomPlot之用户交互
- 封装SlideQQ样式
- 关于缺陷报告
- wpf 关于鼠标换轮放大缩小只有120的问题
- Number of Digit One
- Java程序执行Linux命令
- iOS中各种动画分类
- Nginx Rewrite规则初探
- HDU 5480(前缀和||树状数组)
- hdu 5455__Fang Fang