iOS日常基础记录12

这里这么做的目的,就是对tableVC做一个强引用,还有种做法就是自己写一个属性,强引用这个tableVC,让它不会被释放
readonly的实质是不让调set方法,在当前类能访问下划线的成员变量,在子类中下划线成员变量也是不能访问的
#import 

@interface DragerViewController : UIViewController

@property (nonatomic, weak, readonly)  UIView *mainV;
@property (nonatomic, weak, readonly)  UIView *leftV;

//打开抽屉
- (void)open;
//关闭抽屉
- (void)close;

@end
#import "DragerViewController.h"

@interface DragerViewController ()

@end

@implementation DragerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化工作
    [self addChildView];
    
    //添加手势
    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.mainV addGestureRecognizer:panGes];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)];
    [self.leftV addGestureRecognizer:tap];    
    
}

#define screenW [UIScreen mainScreen].bounds.size.width

#define target screenW * 0.8

- (void)pan:(UIPanGestureRecognizer *)pan {
    //让中间红色View平移
    //获取偏移量
    CGPoint transP = [pan translationInView:self.mainV];
    //根据偏移量,计算当前的位置大小
    [self positionWithOffset:transP.x];
    
    //判断手势的状态
    if (pan.state == UIGestureRecognizerStateEnded) {
        //如果当前x值大于屏幕宽度一半时,自动定位到右侧
        if (self.mainV.frame.origin.x > screenW * 0.5) {
            //计算偏移
            [UIView animateWithDuration:0.25 animations:^{
                CGFloat offset = target - self.mainV.frame.origin.x;
                [self positionWithOffset:offset];
            }];
        }else {
             //否则,关闭抽屉(复位)
            [self close];
        }
    }
    
    //清0
    [pan setTranslation:CGPointZero inView:self.mainV];
}


//打开抽屉
- (void)open {
    [UIView animateWithDuration:0.25 animations:^{
        [self positionWithOffset:target];
        CGRect frame = self.mainV.frame;
        frame.origin.x = target;
        self.mainV.frame = frame;
    }];
}


//关闭抽屉
- (void)close {
    [UIView animateWithDuration:0.25 animations:^{
        //清空形变
        self.mainV.transform = CGAffineTransformIdentity;
        //位置复位
        self.mainV.frame = self.view.bounds;
    }];
}


//根据偏移量,计算当前的位置大小
- (void)positionWithOffset:(CGFloat)offset {
    //平移
    CGRect frame = self.mainV.frame;
    frame.origin.x += offset;
    self.mainV.frame = frame;
    
    if (self.mainV.frame.origin.x <= 0) {
        self.mainV.frame = self.view.bounds;
    }
    
    if (self.mainV.frame.origin.x >= target) {
        CGRect frame = self.mainV.frame;
        frame.origin.x = target;
        self.mainV.frame = frame;
    }
    
    
    //原始比例:1
    //0.9 0.8 0.7
    
    //1 - 0.1 = 0.9
    //1 - 0.2 = 0.8
    //1 - 0.3 = 0.7
    //缩放
    //找最大值,最大值0.3
    //当x值等于屏幕宽度时,变化的值是0.3
    CGFloat scale = self.mainV.frame.origin.x *  0.3 / screenW;
    scale = 1 - scale;
    NSLog(@"%f",scale);
    
    //形变
    self.mainV.transform = CGAffineTransformMakeScale(scale, scale);
}

- (void)addChildView {
    //添加子控件
    UIView *leftV = [[UIView alloc] init];
    leftV.backgroundColor = [UIColor blueColor];
    leftV.frame = self.view.bounds;
    [self.view addSubview:leftV];
    _leftV = leftV;
    
    UIView *mainV = [[UIView alloc] init];
    mainV.backgroundColor = [UIColor redColor];
    mainV.frame = self.view.bounds;
    [self.view addSubview:mainV];
    _mainV = mainV;
}

@end
#import "DragerViewController.h"

@interface XMGViewController : DragerViewController

@end
#import "XMGViewController.h"
#import "TableViewController.h"

@interface XMGViewController ()

@end

@implementation XMGViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //如果想要在原始封装通用当中添加一些属性自己的业务逻辑,继承原来的类.自己的业务逻辑写到子类当中.
    TableViewController *tableVC = [[TableViewController alloc] init];
    tableVC.view.frame = self.mainV.bounds;
    /** 如果说一个控制器的View成为另一个控制器View的子控件.
    //那么当前控制器也要成为另一个控制器的子控制器.
    //这里这么做的目的,就是对tableVC做一个强引用,还有种做法就是自己写一个属性,
     强引用这个tableVC,让它不会被释放 */
    [self.mainV addSubview:tableVC.view];
    [self addChildViewController:tableVC];
    
    [self open];
}

@end

你可能感兴趣的:(iOS日常基础记录12)