iPhone SDK示例代码(本Blog仅用于备忘记录和回顾,请尊重转载与原创作品之作者的劳动果实)

写Log

在Xcode里,点菜单Run>Console就可以看到NSLog的纪录。


NSLog(@"log: %@", myString);

NSLog(@"log: %f", myFloat);

NSLog(@"log: %i", myInt);


图片显示

不需要UI资源绑定,在屏幕任意处显示图片。下面的代码可以被用到任意View里面。


CGRect myImageRect = CGRectMake(0.0f, 0.0f);

UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];

[myImage setImage:[UIImage imageNamed:@"myImage.png"]];

myImage.opaque = YES; //explicitly  opaque for performance

[self.view addSubview:myImage];

[myImage release];


应用程序边框大小

我们应该使用“bounds”来获得应用程序边框。不是用“applicationFrame”."applicationFrame"还包含了一个20像素的

status bar。除非我们需要那额外的20像素的status  bar。


web view

UIWebView类的调用


CGRect  webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);

UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];

[webView setBackgroundColor:[UIColor whileColor]];

NSString *urlAddress = @"http://www.goole.com";

NSURL *url = [NSURL URLWithString:urlAddress];

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[webView loadRequest:requestObj];

[self addSubview:webView];

[webView release];


显示网络激活状态图标

在iPhone的状态栏的左上方显示的一个icon假如在旋转的话,那就说明现在网络正在被使用。


UIApplication *app = [UIApplication sharedApplication];

app.networkActivityIndicatorVisible = YES;//to stop it,set this to NO


Animation:一组图片

连续的显示一组图片


NSArray *myImages = [NSArray arrayWithOnjects:

[UIImage imageNamed:@"myImage1.png"],

[UIImage imageNamed:@"myImage2.png"],

[UIImage imageNamed:@"myImage3.png"],

[UIImage imageNamed:@"myImage4.png"],

nil];


UIImageView *myAnimatedView = [UIImageView alloc];

[myAnimatedView initWithFrame;[self bounds]];

myAnimatedView.animationImages = myImages;

myAnimatedView.animationDuration = 0.25; //seconds

myAnimatedView.animationRepeatCount = 0; // 0=loops forever

[myAnimatedView startAnimating];

[self addSubview:myAnimatedView];

[myAnimatedView release];


让- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;可以接受多点触摸添加以下三行代码


self.view.multipleTouchEnabled = YES;

self.view.exclusiveTouch = YES;

self.view.userInteractionEnabled = YES;


Animation:移动一个对象

让一个对象在屏幕上显示成一个移动轨迹,注意:这个Animation叫"fire and forget".也就是说编程人员不能够在animation

过程中获得任何信息(比如当前的位置)。假如你需要这个信息的话,那么就需要通过animate和定时器在必要的时候去调整x&y坐标。


CABasicAnimation *theAnimation;

theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];

theAnimation.duration = 1;

theAnimation.repeatCount = 2;

theAnimation.autoreverses = YES;

theAnimation.fromValue = [NSNumber numberWithFloat:0];

theAnimation.toValue = [NSNumber numberWithFloat:-60];

[view.layer addAnimation:theAnimation forKey:@"anima"];


NSString和int类型转换

下面的这个例子让一个text label 显示的一个整形的值


currentScoreLabel.text = [NSString stringWithFormat:@"%d", currentScore];


正则表达式(RegEx)

当前的framework还不支持RegEx。开发人员还不能在iPhone上使用包括NSPredicate在类的regex。

但是自模拟器上是可以使用NSPredicate的,就是不能在真机上支持。

 

可以拖动的对象items

下面展示如何简单的创建一个可以拖拽的image对象:

1.创建一个新的类来继承UIImageView。

 

@interface myDraggableImage:UIImageView{

}

 

2.在新的磊实现的时候添加两个方法:

 

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{

 

//Retrieve the touch point

CGPoint pt = [[touches anyObject]locationInView:self];

startLocation = pt;

[[self superview]bringSubviewToFront:self];

}

 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event{

 

//Move relative to the original touch point

CGPoint pt = [[touches anyObject]locationInView:self];

CGRect frame = [self frame];

frame.origin.x += pt.x - startLocation.x;

frame.origin.y += pt.y - startLocation.y;

[self setFrame:frame];

}

 

3.现在再创建一个新的image加到我们刚创建的UIImageView里面,就可以展示了。

 

dragger = [[myDraggableImage alloc] initWithFrame:myDragRect];

[dragger setImage:[UIIImage imageNamed:@"myImage.png"]];

[dragger setUserInteractionEnabled:YES];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值