Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Global suspension Button by iOS

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how iOS implements global hovering buttons. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The details are as follows

Now a lot of app do this global button.

As shown in the above two pictures, complete a global suspension button without scratching out of the screen.

Since it is the overall situation, it is written in AppDelegate.

UIWindow is a special UIView, which is equivalent to a picture frame, while UIView is equivalent to the canvas inside. There is usually only one UIWindow in an app.

AppDelegate.h

Interface AppDelegate: UIResponder @ property (strong, nonatomic) UIWindow * window;@property (strong, nonatomic) UIButton * button;@end

AppDelegate.m

Button lazy load first

-(UIButton*) button {if (! _ button) {_ button = [UIButton buttonWithType:UIButtonTypeCustom]; _ button.frame = CGRectMake (258,450,60,60); / / initial position on the screen [_ button setImage: [UIImage imageNamed:@ "bcl_btn_whole"] forState:UIControlStateNormal];} return _ button;}

Then add it to the window and set the gesture

-(void) createButton {if (! _ button) {_ window = [[UIApplication sharedApplication] keyWindow]; _ window.backgroundColor = [UIColor whiteColor]; [_ window addSubview:self.button]; UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action:@selector (locationChange:)]; pan.delaysTouchesBegan = YES; [_ button addGestureRecognizer:pan];}}

This is to create a global button two seconds after boot.

-(BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {[self performSelector:@selector (createButton) withObject:nil afterDelay:2];}

The most important thing is to set button not to cross out of the screen.

The following four else if are the top and bottom of the screen.

Set a tag value isOVer

If it is beyond the range of the screen, correct it.

-(void) locationChange: (UIPanGestureRecognizer*) p {CGFloat HEIGHT=_button.frame.size.height; CGFloat WIDTH=_button.frame.size.width; BOOL isOver = NO; CGPoint panPoint = [p locationInView: [UIApplication sharedApplication] .windows [0]]; CGRect frame = CGRectMake (panPoint.x, panPoint.y, HEIGHT, WIDTH); NSLog (@ "% HEIGHT. If (p.state = = UIGestureRecognizerStateChanged) {_ button.center = CGPointMake (panPoint.x, panPoint.y);} else if (p.state = = UIGestureRecognizerStateEnded) {if (panPoint.x + WIDTH > KScreenWidth) {frame.origin.x = KScreenWidth-WIDTH; isOver = YES;} else if (panPoint.y + HEIGHT > KScreenHeight) {frame.origin.y = KScreenHeight-HEIGHT; isOver = YES } else if (panPoint.x-WIDTH / 2 < 0) {frame.origin.x = 0; isOver = YES;} else if (panPoint.y-HEIGHT / 2 < 0) {frame.origin.y = 0; isOver = YES } if (isOver) {[UIView animateWithDuration:0.3 animations: ^ {self.button.frame = frame;}];} Thank you for reading! This is the end of the article on "how to achieve the global suspension button in 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, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report