iPhone 游戏中既播放背景音乐又播放特效声音的办法
有时候在 iPhone 游戏中,既要播放背景音乐,同时又要播放比如枪的开火音效。此时您可以试试以下方法
NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:fileNameofType:@"wav"]; //创建音乐文件路径
NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];
AVAudioPlayer* musicPlayer = [[AVAudioPlayer
alloc]
[musicURL release];
[musicPlayer prepareToPlay];
//[musicPlayer setVolume:1]; //设置音量大小
//musicPlayer .numberOfLoops = -1;//设置音乐播放次数 -1 为一直循环
要 导 入 框 架 AVFoundation.framework , 头 文 件 中<AVFoundation/AVFoundation.h>;做成类的话则更方便。
NSNotificationCenter 用于增加回调函数
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(_willBecomeActive) name:UIApplicationDidBecomeActiveNotificationobject:nil];
UINavigationBar 背景 HackLOGO_320×44.png 图片显示在背景上,
更多苹果移动应用开发入门精选文档教程荟萃:http://down.51cto.com/zt/2401
@implementation UINavigationBar (UINavigationBarCategory)- (void)drawRect:(CGRect)rect {
//加入旋转坐标系代码// Drawing code
UIImage *navBarImage = [UIImage imageNamed:@"LOGO_320×44.png"];CGContextRef context = UIGraphicsGetCurrentContext();CGContextTranslateCTM(context, 0.0, self.frame.size.height);CGContextScaleCTM(context, 1.0, -1.0);
CGPoint center=self.center;
CGImageRef cgImage= CGImageCreateWithImageInRect(navBarImage.CGImage,CGRectMake(0, 0, 1, 44));
CGContextDrawImage(context, CGRectMake(center.x-160-80, 0, 80, self.frame.size.height),cgImage);
CGContextDrawImage(context, CGRectMake(center.x-160, 0, 320, self.frame.size.height),navBarImage.CGImage);
CGContextDrawImage(context, CGRectMake(center.x+160, 0, 80, self.frame.size.height),cgImage);
}@end
old code
CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height),navBarImage.CGImage);
清除电话号码中的其他符号(源码)
最近从通讯录读取电话号码,读出得号码如:134-1814-****。而我需要的为 11 位纯数字,一直找方法解决此问题,今天终于找到了。
分享一下„„
代码如下:
NSString *originalString = @"(123) 123123 abc";NSMutableString *strippedString = [NSMutableString
stringWithCapacity:originalString.length];
NSScanner *scanner = [NSScanner scannerWithString:originalString];NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:@"0123456789"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
[strippedString appendString:buffer];}
// --------- Add the following to get out of endless loopelse {
[scanner setScanLocation:([scanner scanLocation] + 1)];}
// --------- End of addition}
NSLog(@"%@", strippedString); // "123123123"
正则判断:字符串只包含字母和数字
NSString *mystring = @"Letter1234";NSString *regex = @"[a-z][A-Z][0-9]";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([predicate evaluateWithObject:mystring] == YES) {//implement
}
修改 UITableview 滚动条颜色的方法
UITableview 的滚动条默认颜色是黑色的,如果 UItableview 背景也是深颜色,则
滚动条会变的很不明显。您可以用下面这行代码来改变滚动条的颜色
self.tableView.indicatorStyle=UIScrollViewIndicatorStyleWhite;当然,最后的 “White” 也可以换成其它颜色。
一:确认网络环境 3G/WIFI
1. 添加源文件和 framework
%@",[m_request.responseHeaders
开发 Web 等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过 Apple 的审(我们的)查的。
更多苹果移动应用开发入门精选文档教程荟萃:http://down.51cto.com/zt/2401
Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。要在应用程序程序中使用 Reachability,首先要完成如下两部:
1.1. 添加源文件:
在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和Reachability.m 拷贝到你的工程中。如下图:
1.2.添加 framework:
将 SystemConfiguration.framework 添加进工程。如下图:
2. 网络状态
Reachability.h 中定义了三种网络状态:typedef enum {
NotReachable = 0,ReachableViaWiFi,ReachableViaWWAN
} NetworkStatus;
因此可以这样检查网络状态:
//无连接
//使用 3G/GPRS 网络
//使用 WiFi 网络
Reachability *r = [Reachability reachabilityWithHostName:@“www.apple.com”];switch ([r currentReachabilityStatus]) {
}
case NotReachable:
// 没有网络连接
break;
case ReachableViaWWAN:
// 使用 3G 网络
break;
case ReachableViaWiFi:
// 使用 WiFi 网络break;
3.检查当前网络环境程序启动时,如果想检测可用的网络环境,可以像这样// 是否 wifi
+ (BOOL) IsEnableWIFI {
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] !=
更多苹果移动应用开发入门精选文档教程荟萃:http://down.51cto.com/zt/2401
NotReachable);}
// 是否 3G
+ (BOOL) IsEnable3G {
return ([[ReachabilitycurrentReachabilityStatus] != NotReachable);
}
例子:
- (void)viewWillAppear:(BOOL)animated {
reachabilityForInternetConnection]
if (([Reachability reachabilityForInternetConnection].currentReachabilityStatus ==NotReachable) &&
NotReachable)) {
([Reachability reachabilityForLocalWiFi].currentReachabilityStatus ==
self.navigationItem.hidesBackButton = YES;
[self.navigationItem setLeftBarButtonItem:nil animated:NO];}
}
4. 链接状态的实时通知
网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户:
Reachability 1.5 版本
// My.AppDelegate.h#import "Reachability.h"
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {NetworkStatus remoteHostStatus;
}
@property NetworkStatus remoteHostStatus;
@end
// My.AppDelegate.m#import "MyAppDelegate.h"
@implementation MyAppDelegate@synthesize remoteHostStatus;
// 更新网络状态
- (void)updateStatus {
self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
}
// 通知网络状态
- (void)reachabilityChanged:(NSNotification *)note {
[self updateStatus];
if (self.remoteHostStatus == NotReachable) {
UIAlertView *alert =initWithTitle:NSLocalizedString(@"AppName", nil)
[[UIAlertView
alloc]
[alert show];
[alert release];}
}
message:NSLocalizedString (@"NotReachable", nil)delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
// 程序启动器,启动网络监视
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// 设置网络检测的站点
[[Reachability sharedReachability] setHostName:@"www.apple.com"];[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
// 设置网络状态变化时的通知函数
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:@"kNetworkReachabilityChangedNotification" object:nil];[self updateStatus];
}
- (void)dealloc {
// 删除通知对象
[[NSNotificationCenter defaultCenter] removeObserver:self];[window release];
[super dealloc];
}
Reachability 2.0 版本
// MyAppDelegate.h
@class Reachability;
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {Reachability *hostReach;
}@end
// MyAppDelegate.m
- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];NSParameterAssert([curReach isKindOfClass: [Reachability class]]);NetworkStatus status = [curReach currentReachabilityStatus];
if (status == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""
}}
message:@"NotReachable"
delegate:nil
cancelButtonTitle:@"YES" otherButtonTitles:nil];[alert show];
[alert release];
- (void)applicationDidFinishLaunching:(UIApplication *)application {// ...
/ 监测网络情况
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)name: kReachabilityChangedNotificationobject: nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"]
retain];
hostReach startNotifer];
// ...}
二:使用 NSConnection 下载数据
1.创建 NSConnection 对象,设置委托对象
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:[NSURL URLWithString:[self urlString]]];
[NSURLConnection connectionWithRequest:request delegate:self];
2. NSURLConnection delegate 委托方法
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError
*)error;
三:使用 NSXMLParser 解析 xml 文件
1. 设置委托对象,开始解析
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; //或者也可以使用 initWithContentsOfURL 直接下载文件,但是有一个原因不这么做:
// It's also possible to have NSXMLParser download the data, by passing it a URL, butthis is not desirable
// because it gives less control over the network, particularly in responding toconnection errors.
[parser setDelegate:self];[parser parse];
2. 常用的委托方法
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qNameattributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementNamenamespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;static NSString *feedURLString = @"http://www.yifeiyang.net/test/test.xml";
3. 应用举例
- (void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error{
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];[parser parse];
NSError *parseError = [parser parserError];
if (parseError && error) {
*error = parseError;}
[parser release];}
Iphone 实现画折线图
iphone 里面要画图一般都是通过 CoreGraphics.framwork 和 QuartzCore.framwork 实现,apple的官方 sdk demon 中包含了 QuartzCore 的基本用法,
具体 demo 请参考 http://developer.apple.com/library/ios/#samplecode/QuartzDemo/
要实现折线图也就把全部的点连起来,movePointLineto,具体的调用里面的 api 就可以实现了,但是画坐标就比较麻烦了,里面需要去转很多,好在国外有人开源了一个画折线图的开发包,首先看看效果吧,具体怎么用可以参考作者 git 版本库中的 wiki。http://github.com/devinross/tapkulibrary/wiki/How-To-Use-This-Library
这个包还提供了其他的很好看的 UI,都可以调来用,但是我们只需要一个画图要把整个包都导进去,工程太大了,既然是开源的那就想办法提取出来吧,原先之前也有人干过这样的事。http://duivesteyn.net/2010/03/07/iphone-sdk-implementing-the-tapku-graph-in-your-application/
在 UIImageView 中旋转图像
float rotateAngle = M_PI;
CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);imageView.transform = transform;
以上代码旋转 imageView, 角度为 rotateAngle, 方向可以自己测试哦!