封装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