• iPhone开发笔记和技巧总结 (一)


    1)iphone程序中实现截屏的一种方法
    在iphone程序中实现截屏的一种方法:
    //导入头文件
    #import QuartzCore/QuartzCore.h
    //将整个self.view大小的图层形式创建一张图片image UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //然后将该图片保存到图片图
    UIImageWriteToSavedPhotosAlbum(image,self,nil,nil); 
    2)Objective-C 画图
    1.颜色和字体
    UIKit提供了UIColor和UIFont类来进行设置颜色和字体,
    UIColor *redColor=【UIColor redColor】;
    【redColor set】;//设置为红色
    UIFont *front=【UIFont systemFontOfSize:14.0】;//获得系统字体
    【myLable setFont:font】;//设置文本对象的字体
    2.drawRect方法
    对于画图,你首先需要重载drawRect方法,然后调用setNeedsDisplay方法让系统画图:
    -(void)drawRect:(CGRect)rect;//在rect指定的区域画图
    -(void)setNeedsDisplay;//让系统调用drawRect画图
    3)延时函数和Timer的使用
    延时函数:
    [NSThread sleepForTimeInterval:5.0]; //暂停5s.
    Timer的使用:
    NSTimer *connectionTimer; //timer对象
    //实例化timer
    self.connectionTimer=[NSTimerscheduledTimerWithTimeInterval:1.5 target:selfselector:@selector(timerFired:) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop]addTimer:self.connectionTimer forMode:NSDefaultRunLoopMode];
    //用timer作为延时的一种方法 
    do{
    [[NSRunLoopcurrentRunLoop]runUntilDate:[NSDatedateWithTimeIntervalSinceNow:1.0]];
    }while(!done);
    //timer调用函数
    -(void)timerFired:(NSTimer *)timer{
    done =YES;
    }
    4)启动界面的制作
    iPhone开发实现splash画面非常简单,做一个全屏的欢迎页的图片,把它命名为Default.png,然后放在Xcode工程的Resource里面。
    在XXXAppDelegate.m程序中,插入如下代码:
    - (BOOL)application:(UIApplication*)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //–inserta delay of 5 seconds before the splash screendisappears–
    [NSThread sleepForTimeInterval:5.0];
    //Override point for customization after applicationlaunch.
    //Add the view controller’s view to the window anddisplay.
    [windowaddSubview:viewController.view];
    [windowmakeKeyAndVisible];
    return YES;
    }
    这样splash页面就停留5秒后,消失了。
    5)关于控制器Controller的思考
    iPhone开发中,只有一个窗口,对应的是多个视图,而视图的组织形式各种各样,关键是要靠控制器来组织各个视图的逻辑关系。大体的关系如下:
    窗体---主控制器(比如说导航控制器),主控制器在窗体里面,拖动过去即可,在AppDelegate中写相关变量的代码---在主控制器下有别的控制器,比如视图控制器,可以通过interfacebuilder来关联根视图什么的----视图控制器相当于一个根视图,可以调用其他的视图---视图中包含类文件(.h,.m)和图形界面文件(.xib)(两个之间必须关联起来。)
    6)翻页效果
    经常看到iPhone的软件向上向下翻页面的效果,其实这个很简单,已经有封装好的相关方法处理。
    //首先设置动画的相关参数
    [UIView beginAnimations:@"Curl"context:nil];
    [UIView setAnimationDuration:1.25]; //时间
    [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];//速度
    //然后设置动画的动作和目标视图
    [UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    参数UIViewAnimationTransitionCurlUp代表向上翻页,如果向下的话UIViewAnimationTransitionCurlDown.
    forView那把当前的视图传进去。
    //最后提交动画
    [UIView commitAnimations];
    7)自定义按钮
    UIButton *Btn;CGRect frame; Btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; //按钮的类型
    [Btn setImage:[UIImage imageNamed:@“aaa.png”] forState:UIControlStateNormal];//设置按钮图片
    Btn.tag = 10; frame.size.width = 59; //设置按钮的宽度
    frame.size.height = 59; //设置按钮的高度
    frame.origin.x =150; //设置按钮的位置
    frame.origin.y =260; [Btn setFrame:frame]; [Btn setBackgroundColor:[UIColor clearColor]]; [Btn addTarget:self action:@selector(btnPressed:)forControlEvents:UIControlEventTouchUpInside]; //按钮的单击事件
    [self.view addSubview:Btn]; [Btn release];-(void)btnPressed:(id)sender {//在这里实现按钮的单击事件}
    8)截取屏幕图片
    //创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400)
    UIGraphicsBeginImageContext(CGSizeMake(200,400));
    //renderInContext 呈现接受者及其子范围到指定的上下文
    [self.view.layerrenderInContext:UIGraphicsGetCurrentContext()];
    //返回一个基于当前图形上下文的图片
    UIImage *aImage =UIGraphicsGetImageFromCurrentImageContext();
    //移除栈顶的基于当前位图的图形上下文
    UIGraphicsEndImageContext();
    //以png格式返回指定图片的数据
    imageData = UIImagePNGRepresentation(aImage);
  • 相关阅读:
    Linux查看物理CPU个数、核数、逻辑CPU个数
    epoll、cpoll、xpoll
    Curl命令简介
    ps:分钟级监控服务内存变化情况
    linux系统/var/log目录下的信息详解
    pthread_create、pthread_join
    【转载】nginx中gzip的各项配置以及配置参数的意思详解
    linux——Nginx——反向代理服务器
    点击复制文本 ctrl+v粘贴
    npm源切换
  • 原文地址:https://www.cnblogs.com/top5/p/2503788.html
Copyright © 2020-2023  润新知