您的位置:首页 > 移动开发 > Objective-C

开发总结知识点

2013-11-22 10:00 281 查看
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);

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画图

延时函数和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;
}

启动界面的制作

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秒后,消失了。

关于控制器Controller的思考
iPhone开发中,只有一个窗口,对应的是多个视图,而视图的组织形式各种各样,关键是要靠控制器来组织各个视图的逻辑关系。大体的关系如下:

窗体---主控制器(比如说导航控制器),主控制器在窗体里面,拖动过去即可,在AppDelegate中写相关变量的代码---在主控制器下有别的控制器,比如视图控制器,可以通过interfacebuilder来关联根视图什么的----视图控制器相当于一个根视图,可以调用其他的视图---视图中包含类文件(.h,.m)和图形界面文件(.xib)(两个之间必须关联起来。)

翻页效果
经常看到iPhone的软件向上向下翻页面的效果,其实这个很简单,已经有封装好的相关方法处理。
//首先设置动画的相关参数
[UIView
beginAnimations:@"Curl"context:nil];
[UIView setAnimationDuration:1.25]; //时间
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];//速度
//然后设置动画的动作和目标视图
[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
参数UIViewAnimationTransitionCurlUp代表向上翻页,如果向下的话UIViewAnimationTransitionCurlDown.
forView那把当前的视图传进去。
//最后提交动画
[UIView commitAnimations];

自定义按钮

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 {

    //在这里实现按钮的单击事件

}
截取屏幕图片

//创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400)

UIGraphicsBeginImageContext(CGSizeMake(200,400));

//renderInContext 呈现接受者及其子范围到指定的上下文

[self.view.layerrenderInContext:UIGraphicsGetCurrentContext()];

  

 //返回一个基于当前图形上下文的图片

 UIImage *aImage =UIGraphicsGetImageFromCurrentImageContext();

 

 //移除栈顶的基于当前位图的图形上下文

UIGraphicsEndImageContext();

//以png格式返回指定图片的数据

imageData = UIImagePNGRepresentation(aImage); 

使用NSTimer与iphone的简单动画,实现飘雪效果

使用NSTimer与iphone的简单动画,实现飘雪效果,这理原理比较简单,就是定时生成一定的雪花图片,然后使用动画的方式向下漂落(我在其它论坛,看到使用path的方式实现的一个云漂来漂去的效果,实际也可以用那种方式实现,这实际就是前面说的动画效果的两种应用)。所以,我们可以在 viewDidLoad事件中,增加一个图片及定时器并启动,这里的pic请在头文件中定义。

-(void)viewDidLoad{

 [super viewDidLoad];

 self.pic = [UIImage imageNamed:@"snow.png"];//初始化图片

 //启动定时器,实现飘雪效果

 [NSTimer scheduledTimerWithTimeInterval:(0.2) target:self selector:@selector(ontime) userInfo:nil repeats:YES];

}

然后再实现定时器定时调用的ontime方法:

-(void)ontime{

 UIImageView *view = [[UIImageView alloc] initWithImage:pic];//声明一个UIImageView对象,用来添加图片

 view.alpha = 0.5;//设置该view的alpha为0.5,半透明的

 int x = round(random()%320);//随机得到该图片的x坐标

 int y = round(random()%320);//这个是该图片移动的最后坐标x轴的

 int s = round(random()%15)+10;//这个是定义雪花图片的大小

 int sp = 1/round(random()%100)+1;//这个是速度

 view.frame = CGRectMake(x, -50, s, s);//雪花开始的大小和位置

 [self.view addSubview:view];//添加该view

 [UIView beginAnimations:nil context:view];//开始动画

 [UIView setAnimationDuration:10*sp];//设定速度

 view.frame = CGRectMake(y, 500, s, s);//设定该雪花最后的消失坐标

 [UIView setAnimationDelegate:self];

 [UIView commitAnimations];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息