ios15模仿Assistive Touch
Xnip2021-12-12_13-47-48.jpg
- (void)viewDidLoad {
[super viewDidLoad];
LJButton *btn = [[LJButton alloc] init];
[btn setBackgroundImage:[UIImage imageNamed:@"Assistive Touch"] forState:UIControlStateNormal];
btn.frame = CGRectMake(5, 100, 50, 50);
btn.layer.cornerRadius = 5;
btn.layer.masksToBounds = YES;
[self.view addSubview:btn];
}
- (BOOL)prefersStatusBarHidden{
return YES;
}
//
// LJButton.m
// Assistive Touch
//
// Created by lujun on 2021/12/3.
//
#import "LJButton.h"
#define PADDING 5
@interface LJButton()
{
CGPoint _beginPoint;
}
@end
@implementation LJButton
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
self->_beginPoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
CGFloat x = currentPoint.x - _beginPoint.x;
CGFloat y = currentPoint.y - _beginPoint.y;
self.center = CGPointMake(self.center.x + x, self.center.y + y);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
CGFloat btnX = self.frame.origin.x;
CGFloat btnY = self.frame.origin.y;
CGFloat btnW = self.frame.size.width;
CGFloat btnH = self.frame.size.height;
CGFloat kScreenW = [UIScreen mainScreen].bounds.size.width;
CGFloat kScreenH = [UIScreen mainScreen].bounds.size.height;
CGFloat marginLeft = btnX;
CGFloat marginRight = kScreenW - btnX - btnW;
CGFloat marginTop = btnY;
CGFloat marginBottom = kScreenH - btnY - btnH;
[UIView animateWithDuration:0.2 animations:^{
CGFloat btnNewX,btnNewY;
if(marginTop < 60){
if(marginLeft < marginRight){
btnNewX = marginLeft < PADDING ? PADDING : btnX;
}else {
btnNewX = marginRight < PADDING ? (kScreenW - btnW - PADDING) : btnX;
}
btnNewY = PADDING;
self.frame = CGRectMake(btnNewX, btnNewY, btnW, btnH);
}
else if (marginBottom < 60) {
if(marginLeft < marginRight){
btnNewX = marginLeft < PADDING ? PADDING : btnX;
}else {
btnNewX = marginRight < PADDING ? (kScreenW - btnW - PADDING) : btnX;
}
btnNewY = kScreenH - btnH - PADDING;
self.frame = CGRectMake(btnNewX, btnNewY, btnW, btnH);
}
else {
btnNewX = (marginLeft < marginRight) ? PADDING : (kScreenW - btnW - PADDING);
btnNewY = btnY;
self.frame = CGRectMake(btnNewX, btnNewY, btnW, btnH);
}
}];
}
@end
网友评论