Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the effect of suspension window by iOS

Shulou Source: shulou.com Published: 2022-06-03 14:55:09 10月28日 Update

This article will explain in detail how to use iOS to achieve the effect of suspension window. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Demand

Add a floating window to the top of an app application, just like the ios system AssistiveTouch can slide left and right, but will eventually stop on the left or right.

Realization idea

Add a UIWindow to the top level of the application view, use this UIWindow as a floating window, add mobile gesture monitoring to the UIWindow, let the floating window move with the finger, and let it move to the side in an animated way when it is released.

Code / / suspension window test / / create a suspension window mwindow = [AssistiveTouch alloc] initWithFrame:CGRectMake (100,200,40,40) imageName:@ "1.png"]; / / ios9 window to set rootview or crash UIViewController * controller = [[UIViewController alloc] init]; mwindow.rootViewController = controller;// shows the suspension window. [self.window makeKeyAndVisible]; / / add moving gestures UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector (locationChange:)]; pan.delaysTouchesBegan = YES; [self addGestureRecognizer:pan]; / / change position-(void) locationChange: (UIPanGestureRecognizer*) p {/ / [[UIApplication sharedApplication] keyWindow] CGPoint panPoint = [p locationInView: [[UIApplication sharedApplication] keyWindow]] If (p.state = = UIGestureRecognizerStateBegan) {[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector (changeColor) object:nil]; _ imageView.alpha = 0.8;} else if (p.state = = UIGestureRecognizerStateEnded) {[self performSelector:@selector (changeColor) withObject:nil afterDelay:4.0];} if (p.state = = UIGestureRecognizerStateChanged) {self.center = CGPointMake (panPoint.x, panPoint.y) } else if (p.state = = UIGestureRecognizerStateEnded) {if (panPoint.x = kScreenHeight-HEIGHT/2-40 & & panPoint.x > = 20+WIDTH/2) {[UIView animateWithDuration:0.2 animations: ^ {self.center = CGPointMake (panPoint.x, kScreenHeight-HEIGHT/2);}];} else if (panPoint.x

< WIDTH/2+15 && panPoint.y >

KScreenHeight-HEIGHT/2) {[UIView animateWithDuration:0.2 animations: ^ {self.center = CGPointMake (WIDTH/2, kScreenHeight-HEIGHT/2);}];} else {CGFloat pointy = panPoint.y

< HEIGHT/2 ? HEIGHT/2 :panPoint.y; [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(WIDTH/2, pointy); }]; } } else if(panPoint.x >

KScreenWidth/2) {if (panPoint.y = kScreenHeight-40-HEIGHT/2 & & panPoint.x

< kScreenWidth-WIDTH/2-20) { [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(panPoint.x, 480-HEIGHT/2); }]; } else if (panPoint.x >

KScreenWidth-WIDTH/2-15 & & panPoint.y

< HEIGHT/2) { [UIView animateWithDuration:0.2 animations:^{ self.center = CGPointMake(kScreenWidth-WIDTH/2, HEIGHT/2); }]; } else { CGFloat pointy = panPoint.y >

KScreenHeight-HEIGHT/2? KScreenHeight-HEIGHT/2: panPoint.y; [UIView animateWithDuration:0.2 animations: ^ {self.center = CGPointMake (320-WIDTH/2, pointy);}] This is the end of the article on "how to achieve the suspended window effect of iOS". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Applications articles movements effects gestures more good practical code location content animation right ideas fingers articles ways time knowledge system Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Redmi Shulou Information Linux Docker