可以使用平移手势来移动UIWindows吗?

时间:2017-11-12 14:31:45

标签: ios cocoa-touch uiwindow

是否可以通过平移手势识别器移动UIWindow?我一直在理解手势是如何工作的问题,并设法让它适用于视图而不是窗口。

1 个答案:

答案 0 :(得分:0)

是的,你可以。

UIWindowUIView的子类,您可以正常添加PanGesture。要移动窗口,更改UIApplication.sharedApplication.delegate.window的框架,它将正常工作。

创建一个新项目,并将AppDelegate.m文件替换为下面的代码。你可以移动窗口。

#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic, strong) UIPanGestureRecognizer* panGesture;
@property (nonatomic, assign) CGPoint lastPoint;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.

  self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
  [self.window addGestureRecognizer:_panGesture];
  _needUpdate = YES;

  return YES;
}

- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture {
  CGPoint point = [panGesture locationInView:self.window];
  CGPoint center = self.window.center;

  if (CGPointEqualToPoint(_lastPoint, CGPointZero)) {
    _lastPoint = point;
  }

  center.x += point.x - _lastPoint.x;
  center.y += point.y - _lastPoint.y;
  self.window.frame = [UIScreen mainScreen].bounds;
  self.window.center = center;

  if (panGesture.state == UIGestureRecognizerStateEnded) {
    _lastPoint = CGPointZero;
  }
}


@end