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

移动开发
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);
【编辑推荐】
  1. 这款iPhone 5竟然能引起骚乱?不是吧!
  2. 装机必备软件评测:DSM记事本iPhone版
  3. iPhone UI有待提高 你同意吗?

 

责任编辑:冰凝儿 来源: 博客园
相关推荐

2011-08-08 14:57:46

iPhone Autoreleas Property

2013-09-12 12:55:53

iOS开发

2009-06-17 14:33:08

java项目开发

2011-07-19 18:11:09

iPhone 开发

2011-08-15 10:35:43

iPhone开发Atomicnonatomic

2011-08-15 10:45:11

iPhone开发delegate

2015-10-14 10:16:26

安安卓开发EventBus

2011-08-15 10:06:22

iPhone开发nib 文件

2011-08-09 17:29:29

iPhone文件屏幕

2021-02-03 09:59:02

鸿蒙HarmonyOS应用开发

2011-07-27 09:33:14

iPhone 网络 Web

2011-08-15 11:31:27

iPhone开发日志

2009-08-27 16:54:59

C#开发技巧

2014-07-03 16:35:38

WebApp开发技巧总结

2010-01-22 16:35:41

C++开发

2015-06-17 10:28:10

WebAPP开发技巧

2015-06-04 10:44:59

WebAPP开发技巧

2013-04-18 10:19:40

iOS开发Xcode调试

2012-04-26 13:26:58

iPhone应用技巧

2021-11-11 17:36:07

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号