平移手势是否可用于移动 UIWindows



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

是的,你可以。

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

相关内容

  • 没有找到相关文章

最新更新