iOS 模拟终端控件

AKSDeviceConsole

AKSDeviceConsole.h

#import <Foundation/Foundation.h>

/**
 *   打印log的控件
 **/
@interface AKSDeviceConsole : NSObject
+ (void)startService;
@end

AKSDeviceConsole.m

#import "AKSDeviceConsole.h"
#import "AppDelegate.h"
#import <QuartzCore/QuartzCore.h>

#define AKS_LOG_FILE_PATH [[AKSDeviceConsole documentsDirectory] stringByAppendingPathComponent:@"ns.log"]
#define APPDELEGATE                                     ((AppDelegate *)[[UIApplication sharedApplication] delegate])

@interface AKSDeviceConsole () {
    UITextView *textView;
}
@end

@implementation AKSDeviceConsole

+ (id)sharedInstance {
    static id __sharedInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __sharedInstance = [[AKSDeviceConsole alloc]init];
    });
    return __sharedInstance;
}

+ (NSMutableString *)documentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
}

- (id)init {
    if (self = [super init]) {
        [self initialSetup];
    }
    return self;
}

- (void)initialSetup {
    [self resetLogData];
    [self addGestureRecogniser];
}

+ (void)startService {
    double delayInSeconds = 0.1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
        [AKSDeviceConsole sharedInstance];
    });
}

- (void)resetLogData {
    [NSFileManager.defaultManager removeItemAtPath:AKS_LOG_FILE_PATH error:nil];
    freopen([AKS_LOG_FILE_PATH fileSystemRepresentation], "a", stderr);
}

- (void)addGestureRecogniser {
    UISwipeGestureRecognizer *recogniser = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(showConsole)];
    [recogniser setDirection:UISwipeGestureRecognizerDirectionRight];
    [APPDELEGATE.window addGestureRecognizer:recogniser];
}

- (void)showConsole {
    if (textView == nil) {
        CGRect bounds = [[UIScreen mainScreen] bounds];
        float yG = 80.0;
        CGRect viewRectTextView = CGRectMake(0, yG, bounds.size.width, bounds.size.height - yG);

        textView = [[UITextView alloc]initWithFrame:viewRectTextView];
        [textView setBackgroundColor:[UIColor blackColor]];
        [textView setFont:[UIFont systemFontOfSize:12]];
        [textView setEditable:NO];
        [textView setTextColor:[UIColor whiteColor]];
        [[textView layer]setOpacity:0.8];

        [APPDELEGATE.window addSubview:textView];
        [APPDELEGATE.window bringSubviewToFront:textView];

        UISwipeGestureRecognizer *recogniser = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(hideWithAnimation)];
        [recogniser setDirection:UISwipeGestureRecognizerDirectionLeft];
        [textView addGestureRecognizer:recogniser];

        [self moveThisViewTowardsLeftToRight:textView duration:0.30];
        [self setUpToGetLogData];
        [self scrollToLast];
    }
}
- (void)hideWithAnimation {
    [self moveThisViewTowardsLeft:textView duration:0.30];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self hideConsole];
    });
}
- (void)hideConsole {
    [textView removeFromSuperview];
    [NSNotificationCenter.defaultCenter removeObserver:self];
    textView  = nil;
}
- (void)scrollToLast {
    NSRange txtOutputRange;
    txtOutputRange.location = textView.text.length;
    txtOutputRange.length = 0;
    textView.editable = YES;
    [textView scrollRangeToVisible:txtOutputRange];
    [textView setSelectedRange:txtOutputRange];
    textView.editable = NO;
}
- (void)setUpToGetLogData {
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:AKS_LOG_FILE_PATH];
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(getData:) name:@"NSFileHandleReadCompletionNotification" object:fileHandle];
    [fileHandle readInBackgroundAndNotify];
}
- (void)getData:(NSNotification *)notification {
    NSData *data = notification.userInfo[NSFileHandleNotificationDataItem];
    if (data.length) {
        NSString *string = [NSString.alloc initWithData:data encoding:NSUTF8StringEncoding];
        textView.editable = YES;
        textView.text = [textView.text stringByAppendingString:string];
        textView.editable = NO;
        double delayInSeconds = 0.1;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
            [self scrollToLast];
        });
        [notification.object readInBackgroundAndNotify];
    }
    else {
        [self performSelector:@selector(refreshLog:) withObject:notification afterDelay:0.1];
    }
}
- (void)refreshLog:(NSNotification *)notification {
    [notification.object readInBackgroundAndNotify];
}
- (void)moveThisViewTowardsLeft:(UIView *)view duration:(float)dur; {
    [UIView animateWithDuration:dur animations: ^{
        [view setFrame:CGRectMake(view.frame.origin.x - [[UIScreen mainScreen]bounds].size.width, view.frame.origin.y, view.frame.size.width, view.frame.size.height)];
    } completion: ^(BOOL finished) {}];
}
- (void)moveThisViewTowardsLeftToRight:(UIView *)view duration:(float)dur; {
    CGRect original = [view frame];
    [view setFrame:CGRectMake(view.frame.origin.x - [[UIScreen mainScreen]bounds].size.width, view.frame.origin.y, view.frame.size.width, view.frame.size.height)];
    [UIView animateWithDuration:dur animations: ^{
        [view setFrame:original];
    } completion: ^(BOOL finished) {}];
}

@end
时间: 2024-07-30 22:15:37

iOS 模拟终端控件的相关文章

模拟select控件&amp;&amp;显示单击的坐标&amp;&amp;用户按下键盘,显示keyCode

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

无比迅速敏捷地开发IOS超精美控件

目录 前言 设计 编码 PaintCode 前言 自从人生第一篇博客<IOS中的预编译指令的初步探究>问世以来 浏览量竟然达到了360多,(路过的大神勿笑!)这些浏览量使我兴奋异常但又令我黯然神伤,为何我会眼里常含泪水?因为国人伸手党达90%!!!区区只有可怜的三个评论,可怜的三个评论~ 没有鼓励~ 没有鲜花~ 也没有谩骂~ 但是我不哭 因为贱人会笑!我深信: 一日伸手党,bug终身随! 好久没打篮球了,“教练,我想打篮球”. 这次的东西标题为<无比迅速敏捷地开发IOS超精美控件>

前端心得---仿IOS拾取器控件(转轮控件)

希望做一个类似IOS拾取器的控件,在IOS上该控件的效果是这样的:,我也把该效果称之为为轮子效果. 要实现这个效果,能够用到的技术点非常简单,无非是transform的translate3d和rotate,不过要想很好的实现,还要建立一个精确的数学模型,来解决如何[摆放]的问题.特别是这个效果不是静态的,需要满足鼠标滑动的时,这个轮子要转起来,这就需要仔细思索了.当然,在最开始重点还是要搞清楚自变量是什么.因变量是什么.它们之间的关系是什么以及该需求的一些性质.找到了好的性质,可以减轻工作量,并

ios UILabel(label控件)的详细使用及特殊效果

UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0, 0, 75, 40)];   //声明UIlbel并指定其位置和长宽 label.backgroundColor = [UIColorclearColor];   //设置label的背景色,这里设置为透明色. label.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];   //设置label的字体和字

IOS开发 UIView控件

1.万物皆对象 2.LBS:基于位置的服务(热门) 3.在启动XCODE创建项目的时候最好勾选 git 4.在SB界面中更改UILabel 之类的控件里面的内容,控件的frame会随着内容的大小而改变,但是在右侧的属性栏里面更改则不会影响frame 5.IBAction:SB界面原来叫Interface Builder 缩写为IB, 6.M_PI_4  代表45°  以此类推 // OC语法规定:不允许直接修改 某个对象中结构体属性的成员 ? 1 2 3 4 5 6 7 8 // 1.先取出fr

JS模拟select控件的代码

用JavaScript模拟实现Select下拉选框功能,在本效果的实现上,背景图片起到了非常重要的美化作用,然后用CSS和JS分别对LI进行控制,最终实现漂亮的Select效果. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=&q

ios ios7 取消控件往上移

//判断是否为ios7 取消控件往上移 if ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0) { self.edgesForExtendedLayout = UIRectEdgeNone; } ios ios7 取消控件往上移

IOS UIStepper(步进控件)使用总结

IOS中步进控件的简单使用 初始化控件 UIStepper * step = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; 设置控制器值是否连续触发变化 @property(nonatomic,getter=isContinuous) BOOL continuous; 若设置为YES,则长按会连续触发变化,若设置为NO,只有在按击结束后,才会触发. 设置长按是否一直触发变化 @property(nonatomic

iOS UI基础控件之UIView 详解

UIView 简介 什么是UIView UIView是窗口上的一块区域,是iOS中所有控件的基类,我们在app中所有能看见的都是直接或间接继承与UIView的.我们把UIView叫做视图. UIView的作用 负责内部区域的内容渲染. 负责内部区域的触摸事件. 管理本身的所有子视图. 处理基本的动画. UIView创建与使用 创建UIView //通过frame创建View UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100,