前段時(shí)間總監(jiān)有個(gè)需求讓我實(shí)現(xiàn),就是可伸縮的UIButton
比如:
伸縮UIButton
這個(gè)很簡(jiǎn)單沒有什么好說的。
主要做法就是計(jì)算好view的寬和Button的X.
做完之后總監(jiān)不滿意贯底,認(rèn)為既然可以伸縮常柄,那么就應(yīng)該也可以拖動(dòng)!
好了,又要費(fèi)點(diǎn)事兒了。
所以我們給UIButton 添加一個(gè)擴(kuò)展,在擴(kuò)展文件里添寺,我們重寫UIResponder的方法。
- (void)touchesBegan:(NSSet)touches withEvent:(UIEvent)event;**
- (void)touchesMoved:(NSSet)touches withEvent:(UIEvent)event;**
-(void)touchesEnded:(NSSet)touches withEvent:(UIEvent)event;**
我們分別要重寫這三個(gè)方法懈费,具體三個(gè)方法的實(shí)現(xiàn):
touchesBegan:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
self.highlighted = YES;
if (![objc_getAssociatedObject(self, DragEnableKey) boolValue]) {
return;
}
begincenter=self.superview.center;
UITouch *touch = [touches anyObject];
beginPoint = [touch locationInView:self.superview];
}
touchesMoved:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
self.highlighted = NO;
[super touchesMoved:touches withEvent:event];
if (![objc_getAssociatedObject(self, DragEnableKey) boolValue]) {
return;
}
UITouch *touch = [touches anyObject];
CGPoint nowPoint = [touch locationInView:self];
float offsetX = nowPoint.x - beginPoint.x;
float offsetY = nowPoint.y - beginPoint.y;
self.superview.center = CGPointMake(self.superview.center.x + offsetX, self.superview.center.y + offsetY);
}
touchesEnded:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.superview && [objc_getAssociatedObject(self,AdsorbEnableKey) boolValue] ) {
if (self.highlighted) {
[self sendActionsForControlEvents:UIControlEventTouchDown];
self.highlighted = NO;
}
CGPoint nowPoint = self.superview.center;
float offsetX = nowPoint.x - begincenter.x;
float offsetY = nowPoint.y - begincenter.y;
if (fabsf(offsetX)<5 && fabsf(offsetY)<5) {
[super touchesEnded:touches withEvent:event];
}
float marginLeft = self.superview.frame.origin.x;
float marginRight = self.superview.superview.frame.size.width - self.superview.frame.origin.x - self.superview.frame.size.width;
float marginTop = self.superview.frame.origin.y;
float marginBottom = self.superview.superview.frame.size.height - self.superview.frame.origin.y - self.superview.frame.size.height;
[UIView animateWithDuration:0.125 animations:^(void){
if (marginTop<60) {
self.superview.frame = CGRectMake(marginLeft<marginRight?marginLeft<PADDING?PADDING:self.superview.frame.origin.x:marginRight<PADDING?self.superview.superview.frame.size.width -self.superview.frame.size.width-PADDING:self.superview.frame.origin.x,
PADDING,
self.superview.frame.size.width,
self.superview.frame.size.height);
}
else if (marginBottom<60) {
self.superview.frame = CGRectMake(marginLeft<marginRight?marginLeft<PADDING?PADDING:self.superview.frame.origin.x:marginRight<PADDING?self.superview.superview.frame.size.width -self.superview.frame.size.width-PADDING:self.superview.frame.origin.x,
self.superview.superview.frame.size.height - self.superview.frame.size.height-PADDING,
self.superview.frame.size.width,
self.superview.frame.size.height);
}
else {
self.superview.frame = CGRectMake(marginLeft<marginRight?PADDING:self.superview.superview.frame.size.width - self.superview.frame.size.width-PADDING,
self.superview.frame.origin.y,
self.superview.frame.size.width,
self.superview.frame.size.height);
}
}];
}else{
[super touchesEnded:touches withEvent:event];
}
}
最后要實(shí)現(xiàn)的效果大概就是這樣计露。
最后附上gitHub鏈接,希望能給有同樣需求的你們帶來幫助。