这是一个悬浮的按钮,类似于手机的AssistiveTouch效果,可以全工程悬浮的
http://code.cocoachina.com/view/135532
话不多说,上代码,复制黏贴即可
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0,0,50,50)];
btn.backgroundColor=[UIColor orangeColor];
btn.layer.cornerRadius=25;
btn.center=self.window.rootViewController.view.center;
[self.window.rootViewController.view addSubview:btn];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
[btn addGestureRecognizer:panGestureRecognizer];
- (void) handlePan:(UIPanGestureRecognizer*) recognizer
{
CGPoint translation = [recognizer translationInView:self.window.rootViewController.view];
CGFloat centerX=recognizer.view.center.x+ translation.x;
CGFloat thecenter=0;
recognizer.view.center=CGPointMake(centerX,
recognizer.view.center.y+ translation.y);
[recognizer setTranslation:CGPointZero inView:self.window.rootViewController.view];
if(recognizer.state==UIGestureRecognizerStateEnded|| recognizer.state==UIGestureRecognizerStateCancelled) {
if(centerX>ScreenWidth/2) {
thecenter=ScreenWidth-50/2;
}else{
thecenter=50/2;
}
[UIView animateWithDuration:0.3 animations:^{
recognizer.view.center=CGPointMake(thecenter,
recognizer.view.center.y+ translation.y);
}];
}
}
网友评论