是否可以通过平移手势识别器移动UIWindow
?我一直在理解手势是如何工作的问题,并设法让它适用于视图而不是窗口。
答案 0 :(得分:0)
是的,你可以。
UIWindow
是UIView
的子类,您可以正常添加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