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);

原帖很精彩:http://www.cocoachina.com/bbs/read.php?tid=73570&page=1


1 随机数的使用

        头文件的引用
        #import <time.h>
        #import <mach/mach_time.h>

        srandom()的使用
        srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF));

        
直接使用 random() 来调用随机数

2 在UIImageView 中旋转图像

        float rotateAngle = M_PI;
        CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);
        imageView.transform = transform;
      
        
以上代码旋转imageView, 角度为rotateAngle, 方向可以自己测试哦!


3 在Quartz中如何设置旋转点

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
        imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);


        这个是把旋转点设置为底部中间。记住是在QuartzCore.framework中才得到支持。

4 创建.plist文件并存储

        NSString *errorDesc;  //用来存放错误信息
        NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4]; //NSDictionary, NSData等文件可以直接转化为plist文件

        NSDictionary *innerDict;
        NSString *name;
        Player *player;
        NSInteger saveIndex;
    
        for(int i = 0; i < [playerArray count]; i++) {
              player = nil;
              player = [playerArray objectAtIndex:i];
              if(player == nil)
                     break;
              name = player.playerName;// This "Player1" denotes the player name could also be the computer name
              innerDict = [self getAllNodeInfoToDictionary:player];
              [rootObj setObject:innerDict forKey:name]; // This "Player1" denotes the person who start this game
        }
        player = nil;

        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];

        红色部分可以忽略,只是给rootObj添加一点内容。这个plistData为创建好的plist文件,用其writeToFile方法就可以写成文件。下面是代码
        
        /*得到移动设备上的文件存放位置*/
        NSString *documentsPath = [self getDocumentsDirectory];
        NSString *savePath = [documentsPath stringByAppendingPathComponent:@"save.plist"];
    
        /*存文件*/
        if (plistData) {
                [plistData writeToFile:savePath atomically:YES];
         }
         else {
                NSLog(errorDesc);
                [errorDesc release];
        }


        - (NSString *)getDocumentsDirectory {  
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
                return [paths objectAtIndex:0];  
        }


4 读取plist文件并转化为NSDictionary

        NSString *documentsPath = [self getDocumentsDirectory];
        NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"save.plist"];
        NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];


5 读取一般性文档文件

        NSString *tmp;
        NSArray *lines;
/*将文件转化为一行一行的*/
        lines = [[NSString    stringWithContentsOfFile:@"testFileReadLines.txt"]
                       componentsSeparatedByString:@"\n"];
    
         NSEnumerator *nse = [lines objectEnumerator];
    
         // 读取<>里的内容
         while(tmp = [nse nextObject]) {
                  NSString *stringBetweenBrackets = nil;
                  NSScanner *scanner = [NSScanner scannerWithString:tmp];
                  [scanner scanUpToString:@"<" intoString:nil];
                  [scanner scanString:@"<" intoString:nil];
                  [scanner scanUpToString:@">" intoString:&stringBetweenBrackets];

                  NSLog([stringBetweenBrackets description]);
          }


对于读写文件,还有补充,暂时到此。随机数和文件读写在游戏开发中经常用到。所以把部分内容放在这,以便和大家分享,也当记录,便于查找。

6 隐藏NavigationBar
[self.navigationController setNavigationBarHidden:YES animated:YES];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值