IOS 实现简单抽屉效果

来源:互联网 发布:淘宝网热卖怎么加入 编辑:程序博客网 时间:2024/05/23 02:01
////  CustomView.h//  test04抽屉效果////  Created by mouweng on 17/8/25.//  Copyright © 2017年 mouweng. All rights reserved.//#import <UIKit/UIKit.h>@interface CustomView : UIView{    CGPoint openPointCenter;    CGPoint closePointCenter;}-(id)initWithView:(UIView*)contentview parentView:(UIView*) parentview;@property (nonatomic, strong) UIView *parentView; //抽屉视图的父视图@property (nonatomic, strong) UIView *contenView; //抽屉显示内容的视图@end


////  CustomView.m//  test04抽屉效果////  Created by mouweng on 17/8/25.//  Copyright © 2017年 mouweng. All rights reserved.//#import "CustomView.h"#define OPENCENTERX 220.0#define DIVIDWIDTH 70.0 //OPENCENTERX 对应确认是否打开或关闭的分界线。@implementation CustomView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // Initialization code    }    return self;}- (id)initWithView:(UIView *)contentview parentView:(UIView *)parentview{    self = [super initWithFrame:CGRectMake(0,0,contentview.frame.size.width, contentview.frame.size.height)];        if (self) {        self.contenView = contentview;        self.parentView = parentview;                [self addSubview:contentview];        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]                                                        initWithTarget:self                                                        action:@selector(handlePan:)];        [self addGestureRecognizer:panGestureRecognizer];                UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]                                                        initWithTarget:self                                                        action:@selector(handleTap:)];                [self addGestureRecognizer:tapGestureRecognizer];                openPointCenter = CGPointMake(self.parentView.center.x + OPENCENTERX,                                      self.parentView.center.y);                //NSLog(@"openPointCenter x:%f, openPointCenter y:%f",openPointCenter.x,openPointCenter.y);                    }                return self;}-(void) handlePan:(UIPanGestureRecognizer*) recognizer{    CGPoint translation = [recognizer translationInView:self.parentView];        float x = self.center.x + translation.x;    //NSLog(@"translation x:%f", translation.x);        if (x < self.parentView.center.x) {        x = self.parentView.center.x;    }    self.center = CGPointMake(x, openPointCenter.y);        if(recognizer.state == UIGestureRecognizerStateEnded)    {        [UIView animateWithDuration:0.75                              delay:0.01                            options:UIViewAnimationCurveEaseInOut                         animations:^(void)         {             if (x > openPointCenter.x -  DIVIDWIDTH) {                 self.center = openPointCenter;             }else{                 self.center = CGPointMake(openPointCenter.x - OPENCENTERX,                                           openPointCenter.y);                              }                      }completion:^(BOOL isFinish){                      }];    }        [recognizer setTranslation:CGPointZero inView:self.parentView];}-(void) handleTap:(UITapGestureRecognizer*) recognizer{    [UIView animateWithDuration:0.75                          delay:0.01                        options:UIViewAnimationTransitionCurlUp animations:^(void){                            self.center = CGPointMake(openPointCenter.x - OPENCENTERX,                                                      openPointCenter.y);                        }completion:nil];    }@end


////  ViewController.m//  test04抽屉效果////  Created by mouweng on 17/8/25.//  Copyright © 2017年 mouweng. All rights reserved.//#import "ViewController.h"#import "CustomView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];        CGRect rect = CGRectMake(0, 0,                             self.view.frame.size.width,                             self.view.frame.size.height);    //NSLog(@"w:%f, h:%f", rect.size.width, rect.size.height);    UIImageView *imageleft = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"left.jpg"]];    imageleft.frame = rect;    [self.view addSubview:imageleft];                UIView *contentView = [[UIView alloc] initWithFrame:rect];                UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"index.jpg"]];    imageView.frame = rect;    [contentView addSubview:imageView];        CustomView *customView = [[CustomView alloc] initWithView:contentView                                                   parentView:self.view];    [[customView layer] setShadowOffset:CGSizeMake(10, 10)];    [[customView layer] setShadowRadius:20];    [[customView layer] setShadowOpacity:1];    [[customView layer] setShadowColor:[UIColor blackColor].CGColor];        [self.view addSubview:customView];    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end



原创粉丝点击