zoukankan      html  css  js  c++  java
  • 破壳(2)之触摸

    UIResponder 一个可以接收触摸屏上的触摸事件的对象
    触摸事件 motion事件,Remote 控制事件

    -(BOOL) hasPrefix:(NSString *) //astring;检查字符串是否以astring开头; 
    -(BOOL) hasSuffix:(NSString *) //astring;检查字符串是否以astring结尾; 

    1):触摸事件
    – touchesBegan:withEvent:   当用户触摸到屏幕时调用方法
    – touchesMoved:withEvent:  当用户触摸到屏幕并移动时调用此方法
    – touchesEnded:withEvent:  当触摸离开屏幕时调用此方法
    – touchesCancelled:withEvent:  当触摸被取消时调用此方法


    2):iOS 3.0 + 开始支持motion事件,特别是摇动设备

    – motionBegan:withEvent:       运动开始时执行
    – motionEnded:withEvent:       运动结束时执行
    – motionCancelled:withEvent:  运动被取消时执行

    3):iOS 4.0 + 开始支持远程事件
    – remoteControlReceivedWithEvent:

    控件拖动效果:类似iPhone手机上的Assistive touch控件拖动效果,在整个应用程序内悬浮。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        // Override point for customization after application launch.

        self.window = [[UIWindow alloc]init];

        self.window.frame = [UIScreen mainScreen].bounds;

        ViewController *rootViewCtrl = [[ViewController alloc]init];

        

        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:rootViewCtrl];

        self.window.rootViewController = nav;

        

        _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(165, 310, 44, 44)];

        _imgView.image = [UIImage imageNamed:@"jiameng"];

            [self.window.rootViewController.view addSubview:_imgView];

       

        _imgView.userInteractionEnabled = YES;

        

        [self.window makeKeyAndVisible];

        

        return YES;

    }

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

    {

          

        UITouch *touch = [touches anyObject];

        

        NSLog(@"触摸VIew的宽%f",touch.view.frame.size.width);

        if (touch.view.frame.size.width == self.imgView.frame.size.width ) {

            self.isInView = YES;

        }else{

            self.isInView = NO;

        }

        _beginpoint = [touch locationInView:self.imgView];

        

        [super touchesBegan:touches withEvent:event];

    }

    - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

    {

        if (!self.isInView){

            return;

        }

        

        UITouch *touch = [touches anyObject];

        CGPoint currentPosition = [touch locationInView:self.imgView];

        //偏移量

        float offsetX = currentPosition.x - _beginpoint.x;

        float offsetY = currentPosition.y - _beginpoint.y;

        //移动后的中心坐标

        self.imgView.center = CGPointMake(self.imgView.center.x + offsetX, self.imgView.center.y + offsetY);

        

        //x轴左右极限坐标

        if (self.imgView.center.x > (self.imgView.superview.frame.size.width-self.imgView.frame.size.width/2))

        {

            CGFloat x = self.imgView.superview.frame.size.width-self.imgView.frame.size.width/2;

            self.imgView.center = CGPointMake(x, self.imgView.center.y + offsetY);

        }

        else if (self.imgView.center.x < self.imgView.frame.size.width/2)

        {

            CGFloat x = self.imgView.frame.size.width/2;

            self.imgView.center = CGPointMake(x, self.imgView.center.y + offsetY);

        }

        

        //y轴上下极限坐标

        if (self.imgView.center.y > (self.imgView.superview.frame.size.height-self.imgView.frame.size.height/2))

        {

            CGFloat x = self.imgView.center.x;

            CGFloat y = self.imgView.superview.frame.size.height-self.imgView.frame.size.height/2;

            self.imgView.center = CGPointMake(x, y);

        }

        else if (self.imgView.center.y <= self.imgView.frame.size.height/2)

        {

            CGFloat x = self.imgView.center.x;

            CGFloat y = self.imgView.frame.size.height/2;

            self.imgView.center = CGPointMake(x, y);

        }

    }

     

    1. CGrect screenBounds = [ [UIScreen mainScreen]bounds];//返回的是带有状态栏的Rect  
    2. CGRect viewBounds = [ [UIScreen mainScreen]applicationFrame];//不包含状态栏的Rect

    文件操作




    显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true

    隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false
    输完单击Enter键,退出终端,重新启动Finder就可以了
    重启Finder:鼠标单击窗口左上角的苹果标志-->强制退出-->Finder-->

    复制代码
        //NSCachesDirectory NSCachesDirectory NSLibraryDirectory 
            NSString *homeDirectory = NSHomeDirectory(); //获取程序HOME目录
            NSString *tempDirectory = NSTemporaryDirectory(); //获取程序缓存目录
            NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES); //获取document目录
            NSString *documentPath = [documentPaths objectAtIndex:0];
            NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSAllDomainsMask, YES); //获取cache目录
            NSString *cachePath = [cachesPaths objectAtIndex:0];
    
            NSArray *libraryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSAllDomainsMask, YES); //所有library目录
            NSString *libraryPath = [libraryPaths objectAtIndex:0];
            NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
            [array writeToFile:filePath atomically:YES]; //写入文件
            NSArray *arr = [[NSArray alloc] initWithContentsOfFile: filePath] 读取文件Path];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];       
    
       //创建目录       
    
       [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    
            //创建文件
            NSString *testPath4 = [testDirectory stringByAppendingPathComponent:@"TEST44.txt"];
            NSString *string = @"写入内容,write String";
            [fileManager createFileAtPath:testPath4 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
            //遍历目录
            NSArray *file = [fileManage subpathsAtPath:myDirectory];
            NSArray *files = [fileManage subpathsOfDirectoryAtPath:myDirectory error:nil];
            //更改到待操作的目录下
            [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
            //创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
            NSString * fileName = @"testFileNSFileManager.txt";
            NSData *data = [fileManager contentsAtPath:fileName];
            [fileManager createFileAtPath:fileName contents:data attributes:nil];
            [fileManager removeItemAtPath:fileName  error:nil];
  • 相关阅读:
    Jsoup系列学习(1)-发送get或post请求
    使用httpclient发送get或post请求
    charles 抓取eclipse中的请求
    springmvc学习资料整理
    springmvc和struts2的区别
    Linux常用命令
    Sql Server 2008R2 数据库发布与订阅
    jquery 父、子页面之间页面元素的获取,方法的调用
    封装ajax
    Maven 的安装配置
  • 原文地址:https://www.cnblogs.com/liuxiaokun/p/5531727.html
Copyright © 2011-2022 走看看