最近有做一个点击弹出子按钮控件。
先看效果:
或者:
当时时间比较紧,就考虑如何用最方便的方式做这么个类似之前苹果悬浮辅助键的效果,代码.h:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface QQXYAnimationButton : UIView
@property (strong,nonatomic)UIButton *mainBtn;
@property (strong,nonatomic)NSMutableArray *otherBtns;
@property (strong,nonatomic)NSMutableArray *btnPlaces; //相对位置数组
-(void)openMove; //允许父按钮拖动,暂时没写关闭方法。
//此处也可以添加参数:距离主按钮的距离和角度,然后计算相对位置,有点麻烦。
@end
NS_ASSUME_NONNULL_END
.m:
#import "QQXYAnimationButton.h"
@interface QQXYAnimationButton ()
@property BOOL isShow;
@property BOOL moveOpen;
@end
@implementation QQXYAnimationButton
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.isShow = NO;
self.mainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:self.mainBtn];
[self.mainBtn addTarget:self action:@selector(mainBtnCilcked) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.mainBtn.frame = self.bounds;
}
-(void)openMove
{
if (!self.moveOpen) {
UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(doHandlePanAction:)];
[self addGestureRecognizer:panGestureRecognizer];
self.moveOpen = YES;
}
}
-(void)mainBtnCilcked
{
[self.superview layoutSubviews];
if (!self.isShow) {
for (int i = 0; i<self.otherBtns.count; i++) {
UIButton *btn = self.otherBtns[I];
btn.frame = self.frame;
[self.superview addSubview:btn];
// btn.layer.zPosition = self.layer.zPosition -1;
// self.frame= frame;
[self.superview bringSubviewToFront:self];
}
self.isShow = YES;
[self performSelector:@selector(animationShowNext:) withObject:@(0) afterDelay:0.01];
}else{
self.isShow = NO;
[self performSelector:@selector(animationHideNext:) withObject:@(0) afterDelay:0];
}
}
-(void)animationShowNext:(NSNumber *)target
{
UIButton *btn;
if ([target intValue]<self.otherBtns.count) {
btn = self.otherBtns[target.intValue];
}
if (btn) {
[UIView animateWithDuration:0.15 animations:^{
CGRect toFrame = [self.btnPlaces[target.intValue] CGRectValue];
toFrame = CGRectMake(self.frame.origin.x +toFrame.origin.x, self.frame.origin.y +toFrame.origin.y, self.frame.size.width+toFrame.size.width, self.frame.size.height+toFrame.size.height);
btn.frame = toFrame;
} completion:^(BOOL finished) {
}];
}else{
return;
}
if ([target intValue]<self.otherBtns.count-1) {
NSNumber *next = @([target intValue]+1);
[self performSelector:@selector(animationShowNext:) withObject:next afterDelay:0.1f];
}
}
-(void)animationHideNext:(NSNumber *)target
{
UIButton *btn;
if ([target intValue]<self.otherBtns.count) {
btn = self.otherBtns[target.intValue];
}
if (btn) {
[UIView animateWithDuration:0.15 animations:^{
btn.frame = self.frame;
} completion:^(BOOL finished) {
[btn removeFromSuperview];
}];
}else{
return;
}
if ([target intValue]<self.otherBtns.count-1) {
NSNumber *next = @([target intValue]+1);
[self performSelector:@selector(animationHideNext:) withObject:next afterDelay:0.075f];
}
}
//拖动方法
- (void) doHandlePanAction:(UIPanGestureRecognizer *)paramSender{
if (self.isShow) {
self.isShow = NO;
[self animationHideNext:@(0)];
}
CGPoint point = [paramSender translationInView:self.superview];
NSLog(@"X:%f;Y:%f",point.x,point.y);
paramSender.view.center = CGPointMake(paramSender.view.center.x + point.x, paramSender.view.center.y + point.y);
[paramSender setTranslation:CGPointMake(0, 0) inView:self.superview];
//下面四个方法是判断最大的拖动范围,超出范围时做回弹,
if (paramSender.view.frame.origin.x + paramSender.view.frame.size.width > self.superview.frame.size.width-10 ) {
[UIView animateWithDuration:0.2 animations:^{
self.frame = CGRectMake(self.superview.frame.size.width-self.frame.size.width-10, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
} completion:^(BOOL finished) {
}];
}
if (paramSender.view.frame.origin.y + paramSender.view.frame.size.height > self.superview.frame.size.height-10 ) {
[UIView animateWithDuration:0.2 animations:^{
self.frame = CGRectMake(self.frame.origin.x, self.superview.frame.size.height - self.frame.size.height-10, self.frame.size.width, self.frame.size.height);
} completion:^(BOOL finished) {
}];
}
if (paramSender.view.frame.origin.x < 10) {
[UIView animateWithDuration:0.2 animations:^{
self.frame = CGRectMake(10, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
} completion:^(BOOL finished) {
}];
}
if (paramSender.view.frame.origin.y < 10) {
[UIView animateWithDuration:0.2 animations:^{
self.frame = CGRectMake(self.frame.origin.x, 10, self.frame.size.width, self.frame.size.height);
} completion:^(BOOL finished) {
}];
}
}
@end
示例代码:
-(QQXYAnimationButton *)postBtn
{
if (!_postBtn){
_postBtn = [[QQXYAnimationButton alloc] initWithFrame:CGRectZero];
[_postBtn.mainBtn setBackgroundImage:JFImageNamed(@"group_addNew2") forState:UIControlStateNormal];
[_postBtn.mainBtn setBackgroundColor:[UIColor clearColor]];
NSMutableArray *btnArray = [NSMutableArray new];
NSMutableArray *frameArray = [NSMutableArray new];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setBackgroundImage:JFImageNamed(@"icon_NewTakePicture") forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(toPostPictureCityServer) forControlEvents:UIControlEventTouchUpInside];
[btnArray addObject:btn1];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(-75, -10, 4, 4)]];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setBackgroundImage:JFImageNamed(@"icon_NewTakeVideo") forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(toPostVideoCityServer) forControlEvents:UIControlEventTouchUpInside];
[btnArray addObject:btn2];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(-10, -75, 0, 0)]];
/*
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn3 setBackgroundImage:JFImageNamed(@"icon_NewTakePicture") forState:UIControlStateNormal];
[btn3 addTarget:self action:@selector(toPostVideoCityServer) forControlEvents:UIControlEventTouchUpInside];
[btnArray addObject:btn3];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(15, 60, 0, 0)]];
UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn4 setBackgroundImage:JFImageNamed(@"icon_NewTakeVideo") forState:UIControlStateNormal];
[btn4 addTarget:self action:@selector(toPostVideoCityServer) forControlEvents:UIControlEventTouchUpInside];
[btnArray addObject:btn4];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(60, 15, 0, 0)]];
UIButton *btn5 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn5 setBackgroundImage:JFImageNamed(@"icon_NewTakePicture") forState:UIControlStateNormal];
[btn5 addTarget:self action:@selector(toPostVideoCityServer) forControlEvents:UIControlEventTouchUpInside];
[btnArray addObject:btn5];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(15, -60, 0, 0)]];
UIButton *btn6 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn6 setBackgroundImage:JFImageNamed(@"icon_NewTakeVideo") forState:UIControlStateNormal];
[btn6 addTarget:self action:@selector(toPostVideoCityServer) forControlEvents:UIControlEventTouchUpInside];
[btnArray addObject:btn6];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(-15, 60, 0, 0)]];
UIButton *btn7 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn7 setBackgroundImage:JFImageNamed(@"icon_NewTakePicture") forState:UIControlStateNormal];
[btn7 addTarget:self action:@selector(toPostVideoCityServer) forControlEvents:UIControlEventTouchUpInside];
[btnArray addObject:btn7];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(-60, 15, 0, 0)]];
UIButton *btn8 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn8 setBackgroundImage:JFImageNamed(@"icon_NewTakeVideo") forState:UIControlStateNormal];
[btn8 addTarget:self action:@selector(toPostVideoCityServer) forControlEvents:UIControlEventTouchUpInside];
[btnArray addObject:btn8];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(60, -15, 0, 0)]];
[btnArray addObject:btn1];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(60, 15, 0, 0)]];
[btnArray addObject:btn2];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(15, 60, 0, 0)]];
[btnArray addObject:btn3];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(-15, -60, 0, 0)]];
[btnArray addObject:btn4];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(-15, 60, 0, 0)]];
[btnArray addObject:btn5];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(15, -60, 0, 0)]];
[btnArray addObject:btn6];
[frameArray addObject:[NSValue valueWithCGRect:CGRectMake(-60, -15, 0, 0)]];
*/
[_postBtn openMove];
_postBtn.otherBtns = btnArray;
_postBtn.btnPlaces = frameArray;
}
return _postBtn;
}
网友评论