开发学院手机开发iPhone 开发 常用 IPHONE 编程 基础代码 阅读

常用 IPHONE 编程 基础代码

 2010-03-10 16:08:00 来源:WEB开发网   
核心提示:CGPoint pt = [[touches anyObject] locationInView:self];CGRect frame = [self frame];frame.origin.x += pt.x - startLocation.x;frame.origin.y += pt.y - startLocati

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

上一页  1 2 3 4 5  下一页

Tags:常用 IPHONE 编程

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接