Original address: http://blog.5ibc.net/p/86562.html
Premise: At that time saw someone write this similar assistivetouch demo, but there is a problem, the first can not change the location, the second switch page cannot be used, the third run occasionally crashes. Then he went to the Niang, the forum have looked up some information, and then combined to write a demo.
Idea: the implementation of global needs in the APPDELEGATE.M file Didfinishlaunchingwithoptions method inside the implementation
1. Create a new class that inherits from UIWindow Assistivetouch
Code in the AssistiveTouch.h file
#import <UIKit/UIKit.h> @interface assistivetouch:uiwindow{ UIButton *_button;} -(ID) initWithFrame: (CGRect) frame; @end
Code in the ASSISTIVETOUCH.M file
#import "ASSISTIVETOUCH.H" @interface Assistivetouch () @end @implementation assistivetouch/*//only override DrawRect: If you perform custom drawing.//an empty implementation adversely affects performance during animation.-(void) DrawRect: ( CGRect) Rect {//Drawing code}*/-(ID) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame]; if (self) {self.backgroundcolor = [uicolor Clearcolor]; Self.windowlevel = Uiwindowlevelalert + 1;//This sentence is very important [self makekeyandvisible]; _button = [UIButton buttonwithtype:uibuttontypecustom]; _button.backgroundcolor = [Uicolor Graycolor]; _button.frame = CGRectMake (0, 0, frame.size.width, frame.size.height); _button.layer.cornerradius = FRAME.SIZE.WIDTH/2; [_button addtarget:self Action: @selector (choose) forcontrolevents:uicontroleventtouchupinside]; [Self addsubview:_button];//puts a drag gesture to change the position of the control uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc] Initwit Htarget:self Action: @selector (changepostion:)]; [_button Addgesturerecognizer:pan]; } return self;} Button Event-(void) choose{NSLog (@ "hover window");} Gesture Event--change position-(void) Changepostion: (Uipangesturerecognizer *) pan{cgpoint point = [Pan translationinview:self]; CGFloat width = [UIScreen mainscreen].bounds.size.width; CGFloat height = [UIScreen mainscreen].bounds.size.height; CGRect originalframe = self.frame; if (originalframe.origin.x >= 0 && originalframe.origin.x+originalframe.size.width <= width) {origin Alframe.origin.x + = Point.x; } if (originalframe.origin.y >= 0 && originalframe.origin.y+originalframe.size.height <= height) { ORIGINALFRAME.ORIGIN.Y + = Point.y; } self.frame = Originalframe; [Pan Settranslation:cgpointzero inview:self]; if (pan.state = = Uigesturerecognizerstatebegan) {_button.enabled = NO; }else if (pan.state = = uigesturerecognizerstatechanged) {} else {CGRect frame = Self.frame; Whether the record is out of bounds BOOL isover = NO; if (frame.origin.x < 0) {frame.origin.x = 0; Isover = YES; } else if (Frame.origin.x+frame.size.width > width) {frame.origin.x = Width-frame.size.width; Isover = YES; } if (Frame.origin.y < 0) {frame.origin.y = 0; Isover = YES; } else if (Frame.origin.y+frame.size.height > height) {frame.origin.y = Height-frame.size.height; Isover = YES; } if (isover) {[UIView animatewithduration:0.3 animations:^{self.frame = frame; }]; } _button.enabled = YES; }} @end
#import "AppDelegate.h" #import "AssistiveTouch.h" @interface appdelegate () { //hover box Assistivetouch * _WIN;} @end @implementation appdelegate//Set custom hover box coordinates-(void) setnew{ _win = [[Assistivetouch alloc] initWithFrame: CGRectMake (0, 0, 60, 60)];} -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions { Override point for customization after application launch. This sentence is very important, to first load the Rootview after the completion of the display of the suspended box, if not this sentence, will likely cause the program to crash [self performselector: @selector (setnew) Withobject:nil Afterdelay:3]; [Self.window makekeyandvisible]; return YES;}
You are here: Home»ios»ios Global hover button, similar to the global hover button in iphone Assistivetouch IOS, similar to the Assistivetouch in iphone