抽屉效果实例

#import "ViewController.h"

#define maxY 100
#define screen_width [UIScreen mainScreen].bounds.size.width
#define screen_height [UIScreen mainScreen].bounds.size.height
#define screen_center [UIScreen mainScreen].bounds.size.width * 0.5

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *leftView;
@property (weak, nonatomic) IBOutlet UIView *rightView;
@property (weak, nonatomic) IBOutlet UIView *mainView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self.mainView addGestureRecognizer:panGesture];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tapGesture];
}

- (void)panAction:(UIPanGestureRecognizer*)gesture
{
    CGPoint tranPoint = [gesture translationInView:self.mainView];

    //不能用移动变形,因为这样只能移动x,y
    //self.mainView.transform = CGAffineTransformTranslate(self.mainView.transform, tranPoint.x, 0);
    //[gesture setTranslation:CGPointZero inView:self.mainView];

    self.mainView.frame = [self frameWithOffsetX:tranPoint.x];

    if(self.mainView.frame.origin.x < 0)
    {
        self.rightView.hidden = NO;
    }
    else if(self.mainView.frame.origin.x > 0)
    {
        self.rightView.hidden = YES;
    }

    if(gesture.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.2 animations:^{
            CGFloat targetX;
            CGFloat offsetX;
            CGRect frame;
            if(self.mainView.frame.origin.x > screen_center)
            {
                targetX = screen_width - maxY;
                offsetX = targetX - self.mainView.frame.origin.x;
                frame = [self frameWithOffsetX:offsetX];
            }
            else if(CGRectGetMaxX(self.mainView.frame) < screen_center)
            {
                targetX = - (screen_width - maxY);
                offsetX = targetX - self.mainView.frame.origin.x;
                frame = [self frameWithOffsetX:offsetX];
            }
            else
            {
                frame.origin.x = 0;
                frame.origin.y = 20;
                frame.size.width = screen_width;
                frame.size.height = screen_height;
            }

            self.mainView.frame = frame;
        }];
    }

    [gesture setTranslation:CGPointZero inView:self.mainView];
}

- (void)tapAction:(UITapGestureRecognizer*)gesture
{
    if(self.mainView.frame.origin.x != 0)
    {
        [UIView animateWithDuration:0.2 animations:^{

            CGRect frame;

            frame.origin.x = 0;
            frame.origin.y = 20;
            frame.size.width = screen_width;
            frame.size.height = screen_height;

            self.mainView.frame = frame;
        }];
    }
}

- (CGRect)frameWithOffsetX:(CGFloat)offsetX
{
    CGRect frame = self.mainView.frame;
    frame.origin.x += offsetX;
    frame.origin.y = fabs(frame.origin.x *  maxY / (screen_width)) + 20;
    frame.size.height = screen_height - frame.origin.y * 2 + 20;

    return frame;
}

@end

results matching ""

    No results matching ""