Is it possible to move a UIWindow
via a pan gesture recognizer? I've been having issues understanding how the gestures works and have managed to get it working for views but not windows.
Yes, you can.
UIWindow
is subclass of UIView
and you can add PanGesture
normally. To move window, change frame of UIApplication.sharedApplication.delegate.window
, it will work fine.
Create a new project and replace AppDelegate.m
file with my code below. You can move window.
#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