最近赶了一个项目,项目中涉及到遮罩效果,经过与同事跟网友们的讨论之后,在我那龌龊的代码上修改出了一个遮罩效果。
在viewcontroller里面的我的通过按钮来进行调用的,具体调用自己实现吧
View Code
View Code
1 #import "Mask.h" 2 #define kImageSize 128.0 3 @implementation Mask 4 @synthesize delegate = delegate_; 5 @synthesize speed; 6 @synthesize isStop; 7 8 - (id)init; 9 { 10 if (self = [super init]) { 11 12 } 13 return (self); 14 } 15 //对外开放init方法 16 - (void)maskInitTap:(NSString*)oneName :(NSString*)twoName:(UIView*)maskView 17 { 18 // tempName_ = oneName; 19 maskOneName_ = oneName; 20 maskTwoName_ = twoName; 21 myMaskUIView_ = maskView; 22 23 // maskTimer_ = 0.045; 24 chenMAX_ = NO; 25 //目标显示层图片 26 UIImageView *showImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:maskOneName_]]; 27 showImage.frame = MAXCHENRECT; 28 [myMaskUIView_ addSubview:showImage]; 29 //消失层图片 30 UIImageView *disappearImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:maskTwoName_]]; 31 disappearImage.frame = MAXCHENRECT; 32 [myMaskUIView_ addSubview:disappearImage]; 33 34 //预留覆盖消失层图片“添加指针样式图片” 35 MaskImageView_ = [[UIImageView alloc]init]; 36 MaskImageView_.frame = MINCHENRECT; 37 posiImageView_ = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"指针.png"]]; 38 // [myMaskUIView_ addSubview:posiImageView_]; 39 // posiImageView_.center = myMaskUIView_.center; 40 NSLog(@"x===========%f,y==========%f",zhizhengPosition_.x,zhizhengPosition_.y); 41 //遮罩层图片 42 maskLayer_ = [CALayer layer]; 43 maskLayer_.frame = MINCHENRECT; 44 maskLayer_.contents = (id)[[UIImage imageNamed:@"maskImage.png"]CGImage]; 45 disappearImage.layer.mask = maskLayer_; 46 disappearImage.layer.masksToBounds = YES; 47 //ondrawtap调用部分 48 timerControll_ = [NSTimer scheduledTimerWithTimeInterval:maskTimer_ target:self selector:@selector(OnDrawTap) userInfo:nil repeats:YES]; 49 50 51 isStop = YES; 52 speed = -1; 53 } 54 55 - (void)setSpeedOne 56 { 57 int i = 3; 58 zhizhengPosition_ = CGPointMake(0.2, 5.0); 59 myMaskPoint_.y = myMaskPoint_.y + i; 60 61 MaskImageView_.center = myMaskPoint_; 62 63 posiImageView_.center = CGPointMake(684, myMaskPoint_.y - 383); 64 maskLayer_.position = MaskImageView_.center; 65 66 [myMaskUIView_ addSubview:posiImageView_]; 67 } 68 - (void)setSpeedTwe 69 { 70 int i = 5; 71 zhizhengPosition_ = CGPointMake(0.2, 5.0); 72 myMaskPoint_.y = myMaskPoint_.y + i; 73 MaskImageView_.center = myMaskPoint_; 74 posiImageView_.center = CGPointMake(684, myMaskPoint_.y - 383); 75 maskLayer_.position = MaskImageView_.center; 76 } 77 - (void)setSpeedThree 78 { 79 int i = 15; 80 zhizhengPosition_ = CGPointMake(0.2, 15.0); 81 myMaskPoint_.y = myMaskPoint_.y + i; 82 83 MaskImageView_.center = myMaskPoint_; 84 85 posiImageView_.hidden = YES; 86 posiImageView_.center = CGPointMake(684, myMaskPoint_.y - 453); 87 maskLayer_.position = MaskImageView_.center; 88 } 89 90 //决定遮罩自动下落或者停止的方法//:(CGPoint)acceleration 91 - (void)OnDrawTap 92 { 93 NSLog(@"this is runn%f---------%d",myMaskPoint_.y,speed); 94 myMaskPoint_ = MaskImageView_.center; 95 [self setSpeedOne]; 96 if (myMaskPoint_.y <= 1115 && myMaskPoint_.y > 0) 97 { 98 isStop = NO; 99 if (!isStop) { 100 switch (speed) { 101 case 1: 102 [self setSpeedOne]; 103 break; 104 case 2: 105 [self setSpeedTwe]; 106 break; 107 case 3: 108 [self setSpeedThree]; 109 break; 110 default: 111 break; 112 } 113 } 114 115 if (myMaskPoint_.y >1115) { 116 117 if (delegate_ != nil) { 118 119 [delegate_ isStop]; 120 } 121 NSLog(@"main is stop"); 122 isStop = YES; 123 } 124 125 } 126 else { 127 [self CloseTimer]; 128 isStop = YES; 129 } 130 } 131 //获得chenMAX_的值; 132 - (BOOL)getChenMAX 133 { 134 return chenMAX_; 135 } 136 //设置chenMAX_的值; 137 - (void)setChenMAX:(BOOL)chenMAX 138 { 139 chenMAX_ = chenMAX; 140 } 141 - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer 142 { 143 CGPoint curPos = [gestureRecognizer locationInView:myMaskUIView_]; 144 if (curPos.x > 0 && curPos.x < 72) { 145 return NO; 146 } 147 return YES; 148 } 149 //获得目标显示层与获得消失层图片的名称 150 - (NSString*)getOneName 151 { 152 return maskOneName_; 153 } 154 - (NSString*)getTwoName 155 { 156 return maskTwoName_; 157 } 158 //设置目标显示层与设置消失层图片名称 159 - (void)setOneName:(NSString*)setOneName 160 { 161 maskOneName_ = setOneName; 162 } 163 - (void)setTwoName:(NSString*)setTwoName 164 { 165 maskTwoName_ = setTwoName; 166 } 167 //获取时间与设置时间 168 - (float)getTimer 169 { 170 return maskTimer_; 171 } 172 - (void)setTimer:(float)setTime 173 { 174 maskTimer_ = setTime; 175 } 176 //获得当前点与获得增加点 177 - (CGPoint)getMyMaskPoint 178 { 179 return myMaskPoint_; 180 } 181 - (CGPoint)getPosPosition 182 { 183 return zhizhengPosition_; 184 } 185 //设置当前点与设置增加点 186 - (void)setMyMaskPoint:(CGPoint)setMyMaskPoint 187 { 188 myMaskPoint_ = setMyMaskPoint; 189 } 190 - (void)setPosPosition:(CGPoint)setPosPosition 191 { 192 zhizhengPosition_ = setPosPosition; 193 } 194 //释放方法 195 - (void)CloseTimer 196 { 197 [timerControll_ invalidate]; 198 timerControll_ = nil; 199 200 [posiImageView_ removeFromSuperview]; 201 posiImageView_ = nil; 202 203 [MaskImageView_ removeFromSuperview]; 204 MaskImageView_ = nil; 205 } 206 207 208 209 ////设置下落速度方法,接受传入点控制当前点位置 210 //- (void)setSpeedTap:(float)acceleration 211 //{ 212 // int i; 213 // if (maskTimer_ > 0.02) 214 // { 215 // i = 1; 216 // zhizhengPosition_ = CGPointMake(0.2, 1.0); 217 // myMaskPoint_.y = myMaskPoint_.y + i; 218 // } 219 // else 220 // { 221 // i = 3; 222 // zhizhengPosition_ = CGPointMake(0.2, 3.0); 223 // myMaskPoint_.y = myMaskPoint_.y + i; 224 // } 225 // 226 // MaskImageView_.center = myMaskPoint_; 227 // if (i == 1) { 228 // posiImageView_.center = CGPointMake(684, myMaskPoint_.y - 376); 229 // } 230 // if (i == 3) { 231 // posiImageView_.center = CGPointMake(684, myMaskPoint_.y - 383); 232 // } 233 // maskLayer_.position = MaskImageView_.center; 234 // 235 //} 236 237 238 @end