GitHub地址:https://github.com/runThor/HTAssistiveTouchButton
类似于iOS系统的AssistiveTouch,不过点击效果是弹出子按钮,而不是系统的菜单方式。
效果:
实现:
// HTAssistiveTouchButton.h
#import <UIKit/UIKit.h>
@interface HTAssistiveTouchButton : UIButton
@property (nonatomic, strong) NSMutableArray *childButtons; // 可弹出的子按钮
@end
// HTAssistiveTouchButton.m
#import "HTAssistiveTouchButton.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface HTAssistiveTouchButton ()
@property (nonatomic, assign) BOOL isMoving; // button是否处于移动状态
@property (nonatomic, assign) CGPoint beginPosition; // button触摸开始时的触摸点坐标
@property (nonatomic, assign) float offsetX; // x坐标偏移量
@property (nonatomic, assign) float offsetY; // y坐标偏移量
@end
@implementation HTAssistiveTouchButton
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.childButtons = [[NSMutableArray alloc] init];
[self addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(buttonDrag) forControlEvents:UIControlEventTouchDragInside];
}
return self;
}
// 点击按钮
- (void)buttonClicked {
if (3 != self.childButtons.count) {
return;
}
if (!self.isMoving) {
if ([self.childButtons[0] isHidden]) {
// 弹出子按钮
for (UIButton *btn in self.childButtons) {
[btn setCenter:self.center];
btn.hidden = NO;
}
[UIView animateWithDuration:0.3