练习一下runtime关联属性

刚自己练习了一下runtime属性关联写了一个这个通过cashapLayer  与贝塞尔曲线进行配合绘制出形状  使用观察者模式观察 偏移量由于偏移向下是负值所以用到了数学函数中得取绝对值的办法


练习一下runtime关联属性_第1张图片


#import

#import

#import#importtypedef void(^Le_Start)(NSString *start);

typedef void(^Le_Stops)(NSString *stop);

@interface UIScrollView (UIScrollView_refresh)

@propety (nonatomic, copy)void(^blockname)(id  completion);

-(void)__loadView completion:(void(^)(id completion))completion;

@property (nonatomic , copy)Le_Start le_start;

@property (nonatomic , copy)Le_Stops le_stop;

@property (nonatomic,  weak)id viewController_L;

@property (nonatomic , strong)UIImageView * arrowImageView;

@property (nonatomic , strong)UIView *circleView;

@property (nonatomic ,strong) UIBezierPath *bezier_Path;//贝塞尔曲线属性

@property (nonatomic ,strong) CAShapeLayer *shape_Layer; //shapelayer 属性

//注册观察者

-(void)registObser;

@end

点m文件


//  UIScrollView+UIScrollView_refresh.m

//  ScrollTest

////  Created by Apple on 16/6/30.//  Copyright © 2016年 Apple. All rights reserved.//

#define WS(ws) __weak typeof(self) ws = self

#define SS(ss) __strong typeof(self)ss = self

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

#define SCREENVIEW_HIGHT [UIScreen mainScreen].bounds.size.height

#define WINDOW_W [[UIApplication sharedApplication].delegate window]

#import "UIScrollView+UIScrollView_refresh.h"

@implementation UIScrollView (UIScrollView_refresh)

static  void *Le_StartKey =@"Le_StartKey";

static  void *Le_StopKey  =@"Le_StopKey";

static void * arowImageKey = @"arowImageKey";

static void * viewController_Key =@"viewController_Key";

static void *  circleView_key = @"circleView_key";

static void *  shaplayer_key = @"shaplayer_key";

static void *  bezier_key = @"bezier_key";

static  int count= 0

;-(void)registObser

[self addObserver:self forKeyPath:@"self.contentOffset" options:NSKeyValueObservingOptionNew context:nil];       

self.arrowImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];    self.arrowImageView.bounds = (CGRect){0,0,20,20}; 

  self.arrowImageView.layer.cornerRadius = 10.0f;   

self.arrowImageView.center = CGPointMake(SCREENVIEW_WIDTH/2.0f, -30);    self.arrowImageView.hidden = YES; 

  self.arrowImageView.backgroundColor= [UIColor redColor];    self.arrowImageView.clipsToBounds = YES; 

  self.arrowImageView.layer.cornerRadius = 10.0f;     

   [self.superview addSubview:self.arrowImageView]; 

  self.circleView = [[UIView alloc]init];   

self.circleView.bounds = (CGRect){0,0,20,20};   

self.circleView.center =CGPointMake(SCREENVIEW_WIDTH/2.0f, 30);    self.circleView.backgroundColor = [UIColor redColor];   

self.circleView.clipsToBounds = YES; 

  self.circleView.hidden = YES; 

  [self.superview addSubview:self.circleView];   

self.shape_Layer = [CAShapeLayer layer]; 

  self.bezier_Path = [UIBezierPath bezierPath];   

}



-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context

{


NSLog(@"%f",self.contentOffset.y);

self.circleView.bounds = (CGRect){0,0,20/(fabs(self.contentOffset.y)/30.0f),20/(fabs(self.contentOffset.y)/30.0f)};

self.circleView.layer.cornerRadius = self.circleView.bounds.size.width/2.0f;

if (self.contentOffset.y<-50)

{

[self.superview bringSubviewToFront:self.arrowImageView];

//        WS(ws);

self.arrowImageView.hidden = NO;

self.circleView.hidden    = NO;

self.arrowImageView.center = CGPointMake(SCREENVIEW_WIDTH/2.0f, fabs(self.contentOffset.y)-15);

CGPoint point1 = CGPointMake(self.circleView.center.x-self.circleView.bounds.size.width/2.0f, self.circleView.center.y);

CGPoint point2 = CGPointMake(self.circleView.center.x+self.circleView.bounds.size.width/2.0f, self.circleView.center.y);

CGPoint point3 = CGPointMake(self.arrowImageView.center.x+10, self.arrowImageView.center.y);

CGPoint point4 = CGPointMake(self.arrowImageView.center.x-10, self.arrowImageView.center.y);

[self drawLineWithpoint:point1 point2:point2 point3:point3 point4:point4];

[self.superview bringSubviewToFront:self.arrowImageView];

}

if (self.contentOffset.y<=-100)

{

self.arrowImageView.transform = CGAffineTransformMakeRotation(M_PI);

CGPoint point1 = CGPointMake(self.circleView.center.x-self.circleView.bounds.size.width/2.0f, self.circleView.center.y);

CGPoint point2 = CGPointMake(self.circleView.center.x+self.circleView.bounds.size.width/2.0f, self.circleView.center.y);

CGPoint point3 = CGPointMake(self.arrowImageView.center.x+10, self.arrowImageView.center.y);

CGPoint point4 = CGPointMake(self.arrowImageView.center.x-10, self.arrowImageView.center.y);

[self drawLineWithpoint:point1 point2:point2 point3:point3 point4:point4];

[self.superview bringSubviewToFront:self.arrowImageView];

}if (self.contentOffset.y>-85) {

self.arrowImageView.transform = CGAffineTransformMakeRotation(0);

}

if (self.contentOffset.y<=-40&&count==0)

{

count++;

self.contentInset=UIEdgeInsetsMake(80, 0, 0, 0);

self.le_start(@"kaishi");

}

if (self.contentOffset.y>0)

{

self.le_stop(@"stop");

}

WS(ws);

self.le_stop =^(NSString *stop){

count =0;

[UIView animateWithDuration:0.2 animations:^{

ws.arrowImageView.center = CGPointMake(SCREENVIEW_WIDTH/2.0f, -30);

ws.arrowImageView.hidden = YES;

ws.circleView.hidden = YES;

[ws.shape_Layer removeFromSuperlayer];

[ws.bezier_Path removeAllPoints];

ws.contentInset=UIEdgeInsetsMake(0, 0, 0, 0);

}];

};

}

-(void)drawLineWithpoint:(CGPoint)pointA point2:(CGPoint)pointB point3:(CGPoint)pointC point4:(CGPoint)pointD

{[self.shape_Layer removeFromSuperlayer];

[self.bezier_Path removeAllPoints];

[self.bezier_Path moveToPoint:pointA];//从A开始

[self.bezier_Path addLineToPoint:pointB];//绘制到B

[self.bezier_Path addLineToPoint:pointC];

[self.bezier_Path addLineToPoint:pointD];

self.shape_Layer.fillColor = [UIColor redColor].CGColor;

self.shape_Layer.path = self.bezier_Path.CGPath;

[self.superview.layer addSublayer:self.shape_Layer];

NSLog(@"%@-------%@-----",self.shape_Layer,self.bezier_Path);

}

#pragma mark -关联属性

-(Le_Start)le_start

{return objc_getAssociatedObject(self,&Le_StartKey);

}

-(void)setLe_start:(Le_Start )le_start

{

objc_setAssociatedObject(self, &Le_StartKey, le_start, OBJC_ASSOCIATION_COPY_NONATOMIC);

}

-(UIImageView *)arrowImageView

{

return objc_getAssociatedObject(self,&arowImageKey);

}

-(void)setArrowImageView:(UIImageView *)arrowImageView

{

objc_setAssociatedObject(self, &arowImageKey, arrowImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

-(UIView *)circleView

{

return objc_getAssociatedObject(self,&circleView_key);

}

-(void)setCircleView:(UIView *)circleView

{

objc_setAssociatedObject(self, &circleView_key, circleView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

-(CAShapeLayer *)shape_Layer

{

return objc_getAssociatedObject(self,&shaplayer_key);

}

-(void)setShape_Layer:(CAShapeLayer *)shape_Layer

{

objc_setAssociatedObject(self, &shaplayer_key, shape_Layer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

-(UIBezierPath *)bezier_Path

{

return objc_getAssociatedObject(self,&bezier_key);

}

-(void)setBezier_Path:(UIBezierPath *)bezier_Path

{

objc_setAssociatedObject(self, &bezier_key,bezier_Path, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

-(Le_Stops)le_stop

{

return objc_getAssociatedObject(self,&Le_StopKey);

}

-(void)setLe_stop:(Le_Stops)le_stop

{

objc_setAssociatedObject(self, &Le_StopKey,le_stop, OBJC_ASSOCIATION_COPY_NONATOMIC);

}

-(void)setViewController_L:(id)viewController_L

{

objc_setAssociatedObject(self, &viewController_L, viewController_L, OBJC_ASSOCIATION_ASSIGN);

}

-(id)viewController_L

{

return objc_getAssociatedObject(self, &viewController_Key);

}

-(void)dealloc

{

//移除观察者

[self removeObserver:self forKeyPath:@"self.contentOffset"];

}

@end

你可能感兴趣的:(练习一下runtime关联属性)