模仿苹果手机虚拟键的代码分享,有兴趣的可以玩玩。 下面的是代码,复制粘贴就好.

//
//  ViewController.m
//  SuperDemo
//
//  Created by dongqiangfei on 16/3/3.
//  Copyright © 2016年 dongqiangfei. All rights reserved.
//

#import "ViewController.h"
#define mainScreenWidth [UIScreen mainScreen].bounds.size.width
#define mainScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
{
    UIButton *myBtnView;//虚拟键
    BOOL move;//判断是否是出发拖动手势
    BOOL moveTwo;//判断是否是出发拖动手势
    BOOL yesOrNo;//判断是否是出发拖动手势
    UIView *clickView;//点击home键出来的view

}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    moveTwo = YES;
    
    //虚拟home键
    myBtnView = [UIButton buttonWithType:UIButtonTypeCustom];
    myBtnView.backgroundColor = [UIColor grayColor];
    myBtnView.layer.cornerRadius = 25;//25为方形的一半就成圆的啦
    myBtnView.layer.masksToBounds = YES;
    myBtnView.alpha = 0.4;
    myBtnView.frame = CGRectMake(0, 100, 50, 50);
    myBtnView.layer.borderColor = [UIColor greenColor].CGColor;
    myBtnView.layer.borderWidth = 3;
    myBtnView.userInteractionEnabled = NO;
    [self.view addSubview:myBtnView];
    
    //点击出现的view
    clickView = [[UIView alloc] initWithFrame:CGRectMake(myBtnView.frame.origin.x, myBtnView.frame.origin.y, myBtnView.frame.size.width, myBtnView.frame.size.width)];
    clickView.backgroundColor =[UIColor blackColor];
    clickView.alpha = 0.4;
    clickView.layer.cornerRadius = 15;
    clickView.layer.masksToBounds = YES;//做圆角
    clickView.hidden = YES;
    yesOrNo = YES;
    [self.view addSubview:clickView];
    // Do any additional setup after loading the view, typically from a nib.
}

//获取屏幕开始的点击事件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[touches allObjects] objectAtIndex:0];
    CGPoint touchPoint = [touch locationInView:self.view];
    
    int xbegin = myBtnView.frame.origin.x;
    int yBegin = myBtnView.frame.origin.y;
    int xEnd = myBtnView.frame.origin.x + myBtnView.frame.size.width;
    int yEnd = myBtnView.frame.origin.y + myBtnView.frame.size.height;
    int touchX = touchPoint.x;
    int touchY = touchPoint.y;
    
    if ((touchX > xbegin) && (touchY > yBegin) && (touchX < xEnd) && (touchY < yEnd)) {//如果点击的位置是哪个home键的位置,设置move为yes,拖动的时候触发touchesMoved里的方法
        move = YES;
    }else{
        move = NO;
    }
    moveTwo = YES;
}

//获取屏幕滑动的点击事件
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (move) {//确定是点击的虚拟home位置才开始拖动
        UITouch *touch = [[touches allObjects] objectAtIndex:0];
        CGPoint touchPoint = [touch locationInView:self.view];
        myBtnView.frame = CGRectMake(touchPoint.x-25, touchPoint.y-25, 50, 50);
        moveTwo = NO;
    }
}

//获取屏幕点击结束的事件
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[touches allObjects] objectAtIndex:0];
    CGPoint touchPoint = [touch locationInView:self.view];
    int xbegin = myBtnView.frame.origin.x;
    int yBegin = myBtnView.frame.origin.y;
    int xEnd = myBtnView.frame.origin.x + myBtnView.frame.size.width;
    int yEnd = myBtnView.frame.origin.y + myBtnView.frame.size.height;
    int touchX = touchPoint.x;
    int touchY = touchPoint.y;
    
    int myBtnViewX = 0;
    if ( touchX < (mainScreenWidth/2) ) {//判断虚拟home挪到哪里
        myBtnViewX = 0;
    }else{
        myBtnViewX = self.view.frame.size.width-myBtnView.frame.size.width;
    }
    if (move) {//判断如果点击的位置时虚拟home在挪动他
        [UIView animateWithDuration:0.5 animations:^{
            myBtnView.frame = CGRectMake(myBtnViewX, myBtnView.frame.origin.y , 50, 50);
        }];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        if ((touchX > xbegin) && (touchY > yBegin) && (touchX < xEnd) && (touchY < yEnd) && (moveTwo) && yesOrNo) {//单击了显示 myBtnView 动画
            NSLog(@"单击了");
            clickView.hidden = NO;
            myBtnView.hidden = YES;
            clickView.frame = CGRectMake(mainScreenWidth/8, mainScreenHeight/2-mainScreenWidth*3/8 , mainScreenWidth*3/4, mainScreenWidth*3/4);
        }else{//单击了挪动 myBtnView 的动画
            clickView.frame = CGRectMake(myBtnView.frame.origin.x, myBtnView.frame.origin.y, myBtnView.frame.size.width, myBtnView.frame.size.width);
        }
    } completion:^(BOOL finished) {
        if ((touchX > xbegin) && (touchY > yBegin) && (touchX < xEnd) && (touchY < yEnd) && (moveTwo) && yesOrNo) {
            moveTwo = NO;
            yesOrNo = NO;
        }else{//挪动 myBtnView 的动画 完成之后隐藏掉它
            clickView.hidden = YES;
            myBtnView.hidden = NO;
            moveTwo = YES;
            yesOrNo = YES;
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值