iPhone SDK Examples

by cplus98 - 6월 15, 2009
Article
No Gravatar

Logging

In Xcode, click Run > Console to see NSLog statements.

NSLog(@”log: %@ “, myString); NSLog(@”log: %f “, myFloat); NSLog(@”log: %i “, myInt);

Display Images

Display an image anywhere on the screen, without using UI Builder. You can use this for other types of views as well.

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.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];

Application Frame

Use “bounds” instead of “applicationFrame” — the latter will introduce a 20 pixel empty status bar (unless you want that..)

Web view

A basic UIWebView.

CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0); UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame]; [webView setBackgroundColor:[UIColor whiteColor]]; NSString *urlAddress = @”http://www.google.com”; NSURL *url = [NSURL URLWithString:urlAddress]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; [self addSubview:webView]; [webView release];

Display the Network Activity Status Indicator

This is the rotating icon displayed on the iPhone status bar in the upper left to indicate there is background network activity taking place.

UIApplication* app = [UIApplication sharedApplication]; app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO

Animation: Series of images

Show a series of images in succession

NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"myImage1.png"], [UIImage imageNamed:@"myImage2.png"], [UIImage imageNamed:@"myImage3.png"], [UIImage imageNamed:@"myImage4.gif"], 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];

Animation: Move an object

Show something moving across the screen. Note: this type of animation is “fire and forget” — you cannot obtain any information about the objects during animation (such as current position). If you need this information, you will want to animate manually using a Timer and adjusting the x&y coordinates as necessary.

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:@"animateLayer"];

NSString and int

The following example displays an integer’s value as a text label:

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

Regular Expressions (RegEx)

There is currently no easy way to do RegEx with the framework. You cannot use any regex involving NSPredicate on iPhone — it may work in Simulator, but does not work on the device!

Draggable items

Here’s how to create a simple draggable image.
1. Create a new class that inherits from UIImageView

@interface myDraggableImage : UIImageView { }

2. In the implementation for this new class, add the 2 methods:

- (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. Now instantiate the new class as you would any other new image and add it to your view

dragger = [[myDraggableImage alloc] initWithFrame:myDragRect]; [dragger setImage:[UIImage imageNamed:@"myImage.png"]]; [dragger setUserInteractionEnabled:YES];

Vibration and Sound

Here is how to make the phone vibrate (Note: Vibration does not work in the Simulator, it only works on the device.)

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Sound will work in the Simulator, however some sound (such as looped) has been reported as not working in Simulator or even altogether depending on the audio format. Note there are specific filetypes that must be used (.wav in this example).

SystemSoundID pmph; id sndpath = [[NSBundle mainBundle] pathForResource:@”mySound” ofType:@”wav” inDirectory:@”/”]; CFURLRef baseURL = (CFURLRef) [[NSURL alloc] initFileURLWithPath:sndpath]; AudioServicesCreateSystemSoundID (baseURL, &pmph); AudioServicesPlaySystemSound(pmph); [baseURL release];

Threading

1. Create the new thread:

[NSThread detachNewThreadSelector:@selector(myMethod) toTarget:self withObject:nil];

2. Create the method that is called by the new thread:

- (void)myMethod { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; *** code that should be run in the new thread goes here *** [pool release]; }

What if you need to do something to the main thread from inside your new thread (for example, show a loading symbol)? UseperformSelectorOnMainThread.

[self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:false];

Reading crash logs

If you’re in the unfortunate position of having to decipher a crash log, navigate here to give them meaning.

Testing

1. In Simulator, click Hardware > Simulate Memory Warning to test. You may need to enable this setting on each new page of your app.
2. Be sure to test your app in Airplane Mode.

Access properties/methods in other classes

One way to do this is via the AppDelegate:

myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate]; [[[appDelegate rootViewController] flipsideViewController] myMethod];

Random Numbers

Use arc4random(). There is also random(), but you must seed it manually, so arc4random() is preferred.

Timers

This timer will call myMethod every 1 second.

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

What if you need to pass an object to myMethod? Use the “userInfo” property.
1. First create the Timer

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myMethod) userInfo:myObject repeats:YES];

2. Then pass the NSTimer object to your method:

-(void)myMethod:(NSTimer*)timer { // Now I can access all the properties and methods of myObject [[timer userInfo] myObjectMethod]; }

To stop a timer, use “invalidate”:

[myTimer invalidate]; myTimer = nil; // ensures we never invalidate an already invalid Timer

Application analytics

When you release, you might want to collect data on how often your app is being used. Most people are using PinchMedia for this. They give you Obj-C code that is easy to add to your app and then view statistics through their website.

Time

Calculate the passage of time by using CFAbsoluteTimeGetCurrent().

CFAbsoluteTime myCurrentTime = CFAbsoluteTimeGetCurrent(); // perform calculations here

Alerts

Show a simple alert with OK button.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@”An Alert!” delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil]; [alert show]; [alert release];

Plist files

Application-specific plist files can be stored in the Resources folder of the app bundle. When the app first launches, it should check if there is an existing plist in the user’s Documents folder, and if not it should copy the plist from the app bundle.

// Look in Documents for an existing plist file NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; myPlistPath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.plist", plistName] ]; [myPlistPath retain]; // If it’s not there, copy it from the bundle NSFileManager *fileManger = [NSFileManager defaultManager]; if ( ![fileManger fileExistsAtPath:myPlistPath] ) { NSString *pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource:plistName ofType:@”plist”]; }

Now read the plist file from Documents

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [paths objectAtIndex:0]; NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:@"myApp.plist"]; NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];

Now read and set key/values

myKey = (int)[[plist valueForKey:@"myKey"] intValue]; myKey2 = (bool)[[plist valueForKey:@"myKey2"] boolValue]; [plist setValue:myKey forKey:@"myKey"]; [plist writeToFile:path atomically:YES];

Info button

Increase the touchable area on the Info button, so it’s easier to press.

CGRect newInfoButtonRect = CGRectMake(infoButton.frame.origin.x-25, infoButton.frame.origin.y-25, infoButton.frame.size.width+50, infoButton.frame.size.height+50); [infoButton setFrame:newInfoButtonRect];

Detecting Subviews

You can loop through subviews of an existing view. This works especially well if you use the “tag” property on your views.

for (UIImageView *anImage in [self.view subviews]) { if (anImage.tag == 1) { // do something } }

Handy References

In Xcode, click Run > Console to see NSLog statements.

NSLog(@”log: %@ “, myString);
NSLog(@”log: %f “, myFloat);
NSLog(@”log: %i “, myInt);

Display Images
Display an image anywhere on the screen, without using UI Builder. You can use this for other types of views as well.

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.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];

Application Frame
Use “bounds” instead of “applicationFrame” — the latter will introduce a 20 pixel empty status bar (unless you want that..)
Web view
A basic UIWebView.

CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = @”http://www.google.com”;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self addSubview:webView];
[webView release];

Display the Network Activity Status Indicator
This is the rotating icon displayed on the iPhone status bar in the upper left to indicate there is background network activity taking place.

UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO

Animation: Series of images
Show a series of images in succession

NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"myImage1.png"],
[UIImage imageNamed:@"myImage2.png"],
[UIImage imageNamed:@"myImage3.png"],
[UIImage imageNamed:@"myImage4.gif"],
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];

Animation: Move an object
Show something moving across the screen. Note: this type of animation is “fire and forget” — you cannot obtain any information about the objects during animation (such as current position). If you need this information, you will want to animate manually using a Timer and adjusting the x&y coordinates as necessary.

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:@"animateLayer"];

NSString and int
The following example displays an integer’s value as a text label:

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

Regular Expressions (RegEx)
There is currently no easy way to do RegEx with the framework. You cannot use any regex involving NSPredicate on iPhone — it may work in Simulator, but does not work on the device!
Draggable items
Here’s how to create a simple draggable image.
1. Create a new class that inherits from UIImageView

@interface myDraggableImage : UIImageView {
}

2. In the implementation for this new class, add the 2 methods:

- (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. Now instantiate the new class as you would any other new image and add it to your view

dragger = [[myDraggableImage alloc] initWithFrame:myDragRect];
[dragger setImage:[UIImage imageNamed:@"myImage.png"]];
[dragger setUserInteractionEnabled:YES];

Vibration and Sound
Here is how to make the phone vibrate (Note: Vibration does not work in the Simulator, it only works on the device.)

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Sound will work in the Simulator, however some sound (such as looped) has been reported as not working in Simulator or even altogether depending on the audio format. Note there are specific filetypes that must be used (.wav in this example).

SystemSoundID pmph;
id sndpath = [[NSBundle mainBundle]
pathForResource:@”mySound”
ofType:@”wav”
inDirectory:@”/”];
CFURLRef baseURL = (CFURLRef) [[NSURL alloc] initFileURLWithPath:sndpath];
AudioServicesCreateSystemSoundID (baseURL, &pmph);
AudioServicesPlaySystemSound(pmph);
[baseURL release];

Threading
1. Create the new thread:

[NSThread detachNewThreadSelector:@selector(myMethod)
toTarget:self
withObject:nil];

2. Create the method that is called by the new thread:

- (void)myMethod {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

*** code that should be run in the new thread goes here ***

[pool release];
}

What if you need to do something to the main thread from inside your new thread (for example, show a loading symbol)? Use performSelectorOnMainThread.

[self performSelectorOnMainThread:@selector(myMethod)
withObject:nil
waitUntilDone:false];

Reading crash logs
If you’re in the unfortunate position of having to decipher a crash log, navigate here to give them meaning.
Testing
1. In Simulator, click Hardware > Simulate Memory Warning to test. You may need to enable this setting on each new page of your app.
2. Be sure to test your app in Airplane Mode.
Access properties/methods in other classes
One way to do this is via the AppDelegate:

myAppDelegate *appDelegate
= (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[[[appDelegate rootViewController] flipsideViewController] myMethod];

Random Numbers
Use arc4random(). There is also random(), but you must seed it manually, so arc4random() is preferred.
Timers
This timer will call myMethod every 1 second.

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

What if you need to pass an object to myMethod? Use the “userInfo” property.
1. First create the Timer

[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(myMethod)
userInfo:myObject
repeats:YES];

2. Then pass the NSTimer object to your method:

-(void)myMethod:(NSTimer*)timer {

// Now I can access all the properties and methods of myObject
[[timer userInfo] myObjectMethod];
}

To stop a timer, use “invalidate”:

[myTimer invalidate];
myTimer = nil; // ensures we never invalidate an already invalid Timer

Application analytics
When you release, you might want to collect data on how often your app is being used. Most people are using PinchMedia for this. They give you Obj-C code that is easy to add to your app and then view statistics through their website.
Time
Calculate the passage of time by using CFAbsoluteTimeGetCurrent().

CFAbsoluteTime myCurrentTime = CFAbsoluteTimeGetCurrent();
// perform calculations here

Alerts
Show a simple alert with OK button.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@”An Alert!”
delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil];
[alert show];
[alert release];

Plist files
Application-specific plist files can be stored in the Resources folder of the app bundle. When the app first launches, it should check if there is an existing plist in the user’s Documents folder, and if not it should copy the plist from the app bundle.

// Look in Documents for an existing plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
myPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: @"%@.plist", plistName] ];
[myPlistPath retain];

// If it’s not there, copy it from the bundle
NSFileManager *fileManger = [NSFileManager defaultManager];
if ( ![fileManger fileExistsAtPath:myPlistPath] ) {
NSString *pathToSettingsInBundle = [[NSBundle mainBundle]
pathForResource:plistName ofType:@”plist”];
}

Now read the plist file from Documents

NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [documentsDirectoryPath
stringByAppendingPathComponent:@"myApp.plist"];
NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];

Now read and set key/values

myKey = (int)[[plist valueForKey:@"myKey"] intValue];
myKey2 = (bool)[[plist valueForKey:@"myKey2"] boolValue];

[plist setValue:myKey forKey:@"myKey"];
[plist writeToFile:path atomically:YES];

Info button
Increase the touchable area on the Info button, so it’s easier to press.

CGRect newInfoButtonRect = CGRectMake(infoButton.frame.origin.x-25,
infoButton.frame.origin.y-25, infoButton.frame.size.width+50,
infoButton.frame.size.height+50);
[infoButton setFrame:newInfoButtonRect];

Detecting Subviews
You can loop through subviews of an existing view. This works especially well if you use the “tag” property on your views.

for (UIImageView *anImage in [self.view subviews]) {
if (anImage.tag == 1) {
// do something
}
}

Handy References
Official Apple How-To’s
Learn Objective-C

출처 : http://www.iphoneexamples.com/

0
Tags: ,

XML parser with iPhone

by cplus98 - 5월 20, 2009
iPhone Development
No Gravatar

iPhone에서 제공하는 xml 파서

해더파일 설정 및 라이브러리 설정 방법

http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html

0

iPhone 2nd 사양

by cplus98 - 5월 20, 2009
iPhone Development
No Gravatar

————————————————————-
textures
————————————————————-

1. min size
=> 8×8

2. max size
=> 1024×1024

3. alpha allowed
=> yes, but suggest using alpha masks when performance is needed

4. normal mapping
=> no (no shaders, and only 2 texture slots)

5. specular mapping
=> no (no shaders, and only 2 texture slots)

6. burst, saturate, modulate etc.
=> modulate and saturate only, when not using lightmaps (because only 2 texture slots)

7. environment/sphere mapping
=> Unfortunately not

8. texture clips
=> yes

————————————————————-
shadows
————————————————————-

1. dynamic shadows possible
2. dynamic shadow resolution (512->4096)
3. impact on performance (forest with dynamic shadows on every tree possible or not)
=> not supported, could be possible but would be too slow… must use “fake shadows” instead.

————————————————————-
in-game video
————————————————————-

1. size
2. framerate
3. codec/compression
=> not supported in current firmware… really hope Apple will open the functionality one day… (only way to play movies is by using their media player, fullscreen only, with playback controls when touching the screen)

————————————————————-
sound
————————————————————-

1. max length
=> unlimited if a Music (because using steaming), but quite slow. Suggest using Sounds, with short sounds (max 10sec)

2. count limit per sound bank
=> unlimited

3. samplerate, bitrate
=> 16bits, mono, 44100 Hz or less (lesser will be faster)

————————————————————-
particle systems
————————————————————-

1. max particle count per scene
=> depends on the screen size of the particles (alpha blending limitations)

2. max particle count per emitter
=> no more than 256 if you want good performances, but again it depends on the screen size f the particles

————————————————————-
poly count, animation
————————————————————-

1. max polys visible at the same time
=> 7.000 for a gool frame rate, but tested with lot more, and it “works” (don’t crash)

2. max polys per scene
=> limited to 24Mb Texture + Vertex Buffers memory

3. max poly per object
=> depends on the number of visible objects, but again, “lesser is better”

4. animation limit per anim bank
=> none

5. bones count limit
=> “lesser is better” but no limitations (from 0 to 16 is a good value)

6. bones per vertex limit
=> really try to use 1 or 2, but limited to 4.

참고 : http://developer.stonetrip.com/index.php?option=com_fireboard&Itemid=2&func=view&id=5755&catid=39&limit=6&limitstart=0

0

저작권 문제없이 사용할 수 있는 효과음

by cplus98 - 4월 30, 2009
미분류
No Gravatar

저작권 문제없는 음원을 다운로드 할 수 있다.

http://www.freesound.org/

www.soundsnap.com

www.soundsnap.com은 회원가입시 월 5건의 오디오 클립을 무료도 다운로드 할 수 있다.

또한 월 정액으로 무한정 받을 수도있다.

편법으로는 미리듣기 기능은 제약없이 사용할 수 있으므로

Soundflower 와 같은 오디오 캡쳐 프로그램 (프리웨어)을 사용하여 받을 수도 있다.

음원 소스가 MP3 라면 switch와 같은 프리웨어를 이용해서 변환시킬 수 있다.

음원 편집은 Adobe Soundbooth Trial 버전으로 해결할 수 있다.

(자체적으로 제공되는 오디오 클립도 함께 쓸 수 있어서 편하다)

iPhone SDK에서 System Audio Services로 처리할 수 있는 짧은 효과음은 다음과 같은 조건을 갖는다.

- 30초 이내여야 할 것

- 확장자가 .caf, .aif, .wav 중 하나일 것

- 오디오 포맷이 PCM 혹은 IMA/ADPCM (IMA4) 일 것

경험상 caf 나 aif 보다는 wav 가 사용하기 훨씬 수월했고,

mono, 16bit, 22k 형태로 저장하면 음질과 용량 측면에서 적절한 것 같다.

0

효과음 제작도구

by cplus98 - 4월 28, 2009
미분류
No Gravatar

http://thirdcog.eu/apps/cfxr

간단한 효과음을 만드는데 좋다.

0
Tags: