#import <UIKit/UIKit.h>
@protocol SafeCenterButtonDelegate <NSObject>
- (void)safeCenterButtonDidSelect;
@end
@interface SafeCenterButton : UIView
@property (nonatomic, weak) id<SafeCenterButtonDelegate> delegate;
- (instancetype)initWithImage:(NSString *)name top:(CGFloat)top bottom:(CGFloat)bottom frame:(CGRect)frame;
@end
#import "SafeCenterButton.h"
@interface SafeCenterButton ()
{
//是否移动了,移动了取消点击事件
CGFloat moveStepX;
CGFloat moveStepY;
CGFloat minTop;
CGFloat maxTop;
}
@end
@implementation SafeCenterButton
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return NO;
}
- (instancetype)init {
CGFloat bottom = 100 + 38;
if (IS_iPhoneX) {
bottom += 20;
}
return [self initWithImage:@"safecenter_main" top:NavigationBarHeight bottom:bottom frame:CGRectMake(0, ScreenHeight - bottom, 110, 38)];
}
- (instancetype)initWithImage:(NSString *)name top:(CGFloat)top bottom:(CGFloat)bottom frame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
minTop = top;
maxTop = ScreenHeight - bottom;
self.frame = frame;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
imageView.frame = self.bounds;
[self addSubview:imageView];
}
return self;
}
- (void)presentToSafeCenter {
if ([self.delegate respondsToSelector:@selector(safeCenterButtonDidSelect)]) {
[self.delegate safeCenterButtonDidSelect];
}
}
- (void)moveToDefaultPosition {
CGRect frame = self.frame;
if (frame.origin.x <= (ScreenWidth - frame.size.width) / 2) {
frame.origin.x = 0;
} else {
frame.origin.x = ScreenWidth - frame.size.width;
}
if (frame.origin.y < minTop) {
frame.origin.y = minTop;
} else if (frame.origin.y > maxTop) {
frame.origin.y = maxTop;
}
[UIView animateWithDuration:TimeInterval_Animation animations:^{
self.frame = frame;
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
moveStepX = 0;
moveStepY = 0;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
if (fabs(moveStepX) < 1 && fabs(moveStepY) < 1) {
[self presentToSafeCenter];
}
__weak typeof(self) weakSelf = self;
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
[weakSelf moveToDefaultPosition];
});
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
if (@available(iOS 9.1, *)) {
CGPoint previousPoint = [touch precisePreviousLocationInView:self];
CGFloat offsetX = currentPoint.x - previousPoint.x;
CGFloat offsetY = currentPoint.y - previousPoint.y;
// NSLog(@"%f %f", offsetX, offsetY);
moveStepX += offsetX;
moveStepY += offsetY;
self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
} else {
}
}
@end
使用
eCenterButton *safeCenterButton = [[SafeCenterButton alloc] initWithImage:@"safecenter_order" top:StatusBarHeight bottom:BottomHeight + 50 + 38 frame:CGRectMake(0, ScreenHeight - 110 - 38 - 20 - BottomHeight, 110, 38)];
safeCenterButton.delegate = self;
[self.view addSubview:safeCenterButton];