; SCREEN_WIDTH/2) {//悬浮窗在屏幕右侧
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(ViewSize, self.center.y);
}];
}else{
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(SCREEN_WIDTH - ViewSize, self.center.y);
}];
}
}
}
self.alpha = 1;
//三秒后隐藏悬浮窗,贴边展示一半
self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:NO];
}
- (void)timerAction{
//隐藏悬浮球
self.alpha = 0.5;
//判断是否移动过悬浮窗
if (!isChangePosition) {
//悬浮窗初始位置在右上角,只有屏幕向右旋转,才需要留出iphone刘海的位置,设备左转刘海在左边,所以不需要做判断
if (self.orientation == UIInterfaceOrientationLandscapeLeft) {//横向home键在左侧,设备右转,刘海在右边
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(SCREEN_WIDTH - [self vg_safeDistanceTop] - ViewSize - 20, self.center.y);
}];
}else{
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(SCREEN_WIDTH, self.center.y);
}];
}
}else{
if (self.orientation == UIInterfaceOrientationLandscapeRight) {//横向home键在右侧,设备左转,刘海在左边
if (self.center.x > SCREEN_WIDTH/2) {//悬浮窗在屏幕右侧
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(SCREEN_WIDTH, self.center.y);
}];
}else{
//悬浮窗在屏幕左侧,留出刘海安全距离
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake([self vg_safeDistanceTop] + ViewSize + 20, self.center.y);
}];
}
}else if(self.orientation == UIInterfaceOrientationLandscapeLeft){//横向home键在左侧,设备右转,刘海在右边
if (self.center.x > SCREEN_WIDTH/2) {//悬浮窗在屏幕右侧
//悬浮窗在屏幕左侧,留出刘海安全距离
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(SCREEN_WIDTH - [self vg_safeDistanceTop] - ViewSize - 20, self.center.y);
}];
}else{
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(0, self.center.y);
}];
}
}else{
if (self.center.x > SCREEN_WIDTH/2) {//悬浮窗在屏幕右侧
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(SCREEN_WIDTH, self.center.y);
}];
}else{
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(0, self.center.y);
}];
}
}
}
//销毁定时器
[self.timer invalidate];
self.timer = nil;
}
/// pan手势
/// @param recognizer recognizer description
- (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer
{
//移动状态
UIGestureRecognizerState recState = recognizer.state;
isChangePosition = YES;
switch (recState) {
case UIGestureRecognizerStateBegan:
self.alpha = 1;
self.imageView.hidden = NO;
break;
case UIGestureRecognizerStateChanged://移动中
{
self.alpha = 1;
CGPoint translation = [recognizer translationInView:self];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
CGRect rect = [self convertRect:self.frame toView:self];
if (CGRectIntersectsRect(self.imageView.frame, rect)) {//在范围内
self.imageView.backgroundColor = UIColor.redColor;
}else{
self.imageView.backgroundColor = UIColor.blueColor;
}
}
break;
case UIGestureRecognizerStateEnded://移动结束
{
self.alpha = 0.5;
CGPoint stopPoint = CGPointMake(0, SCREEN_HEIGHT / 2);
//判断按钮贴靠在屏幕的左边还是右边
if (recognizer.view.center.x < SCREEN_WIDTH / 2) {
stopPoint = CGPointMake(ViewSize/2, recognizer.view.center.y);
}else{
//贴靠在右边
stopPoint = CGPointMake(SCREEN_WID |