項目中為了語音功能的更好體驗,加了個懸浮按鈕腻菇,網(wǎng)上找了好久耗式,最終集成了一個懸浮按鈕,效果還不錯五芝。
參考Demo
自定義個UIButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
//觸摸-清掃
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
MoveEnabled = NO;
[super touchesBegan:touches withEvent:event];
if (!MoveEnable) {
return;
}
UITouch *touch = [touches anyObject];
beginpoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
MoveEnabled = YES;//單擊事件可用
if (!MoveEnable) {
return;
}
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self];
//偏移量
float offsetX = currentPosition.x - beginpoint.x;
float offsetY = currentPosition.y - beginpoint.y;
//移動后的中心坐標
self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
//x軸左右極限坐標
if (self.center.x > (self.superview.frame.size.width-self.frame.size.width/2)) {
CGFloat x = self.superview.frame.size.width-self.frame.size.width/2;
self.center = CGPointMake(x, self.center.y + offsetY);
}else if (self.center.x < self.frame.size.width/2){
CGFloat x = self.frame.size.width/2;
self.center = CGPointMake(x, self.center.y + offsetY);
}
//y軸上下極限坐標
if (self.center.y > (self.superview.frame.size.height-self.frame.size.height/2)) {
CGFloat x = self.center.x;
CGFloat y = self.superview.frame.size.height-self.frame.size.height/2;
self.center = CGPointMake(x, y);
}else if (self.center.y <= self.frame.size.height/2){
CGFloat x = self.center.x;
CGFloat y = self.frame.size.height/2;
self.center = CGPointMake(x, y);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!MoveEnable) {
return;
}
if (self.center.x >= self.superview.frame.size.width/2) {//向右側移動
//偏移動畫
[UIView beginAnimations:@"move" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
self.frame=CGRectMake(335.f,self.center.y-20, 40.f,40.f);
//提交UIView動畫
[UIView commitAnimations];
}else{//向左側移動
[UIView beginAnimations:@"move" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
self.frame=CGRectMake(0.f,self.center.y-20, 40.f,40.f);
//提交UIView動畫
[UIView commitAnimations];
}
//不加此句話痘儡,UIButton將一直處于按下狀態(tài)
[super touchesEnded: touches withEvent: event];
}
//外界因素取消touch事件,如進入電話
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
MyButton實現(xiàn)
#import "ViewController.h"
#import "MyButton.h"
@interface ViewController ()
{
UIView *tabBarView;
MyButton *myButton;
BOOL flag; //控制tabbar的顯示與隱藏標志
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bj_bg"]];
}
//做了修改 設置tab bar
- (void)addCustomElements
{
myButton = [MyButton buttonWithType:UIButtonTypeCustom];
myButton.MoveEnable = YES;
myButton.frame = CGRectMake(335, 300, 40, 40);
//TabBar上按鍵圖標設置
[myButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"40.png"]] forState:UIControlStateNormal];
[myButton setTag:10];
flag = NO;//控制tabbar的顯示與隱藏標志 NO為隱藏
[myButton addTarget:self action:@selector(tabbarbtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
[self _initTabBar];
}
//初始化tabbar
-(void)_initTabBar
{
//tab bar view 始終居中顯示
tabBarView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-100, self.view.frame.size.height/2-100, 200 , 200)] ;
//view 設置半透明 圓角樣式
tabBarView.layer.cornerRadius = 10;//設置圓角的大小
tabBarView.layer.backgroundColor = [[UIColor blackColor] CGColor];
tabBarView.alpha = 0.8f;//設置透明
tabBarView.layer.masksToBounds = YES;
[self.view addSubview:tabBarView];
//循環(huán)設置tabbar上的button
// NSArray *imgNames = [[NSArray alloc]initWithObjects:@"download.png",@"block.png",@"bluetooth.png",@"file.png", nil];
NSArray *tabTitle = [[NSArray alloc]initWithObjects:@"download",@"block",@"bluetooth",@"file", nil];
for (int i=0; i<4; i++) {
CGRect rect;
rect.size.width = 60;
rect.size.height = 60;
switch (i) {
case 0:
rect.origin.x = 100-30;
rect.origin.y = 40-35;
break;
case 1:
rect.origin.x = 375/2-50;
rect.origin.y = 100-30;
break;
case 2:
rect.origin.x = 100-30;
rect.origin.y = 375/2-50;
break;
case 3:
rect.origin.x = 40-35;
rect.origin.y = 100-30;
break;
}
//設置每個tabView
UIView *tabView = [[UIView alloc]initWithFrame:rect];
[tabBarView addSubview:tabView];
//設置tabView的圖標
UIButton *tabButton = [UIButton buttonWithType:UIButtonTypeCustom];
tabButton.frame = CGRectMake(15, 0, 30, 30);
[tabButton setBackgroundImage:[UIImage imageNamed:[tabTitle objectAtIndex:i]] forState:UIControlStateNormal];
[tabButton setTag:i];
[tabButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[tabView addSubview:tabButton];
//設置標題
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 35, 60, 15)];
titleLabel.font = [UIFont systemFontOfSize:12];
titleLabel.textAlignment = 1;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.text = [tabTitle objectAtIndex:i];
[tabView addSubview:titleLabel];
}
[tabBarView setHidden:YES];
}
//顯示 隱藏tabbar
- (void)tabbarbtn:(MyButton*)btn
{
//在移動的時候不觸發(fā)點擊事件
if (!btn.MoveEnabled) {
if(!flag){
tabBarView.hidden = NO;
flag = YES;
}else{
tabBarView.hidden = YES;
flag = NO;
}
}
}
- (void)buttonClicked:(id)sender
{
NSLog(@"%ld",[sender tag]);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[self addCustomElements];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}