Objective C 从字符串中删除重音

-(NSString*)stringWithoutAccentsFromString:(NSString*)s
{
    if (!s) return nil;
    NSMutableString *result = [NSMutableString stringWithString:s];
    CFStringFold((CFMutableStringRef)result, kCFCompareDiacriticInsensitive, NULL);
    return result;
}

Objective C 使用CoreAnimation加载ViewController

#pragma mark -
#pragma mark Puzzles 
-(void)openPuzzles {
	if (myPuzzleDelegate == nil) {
		myPuzzleDelegate = [[PuzzleDelegate alloc] initWithNibName:@"PuzzleMainView" bundle:nil];
		[self.view addSubview:myPuzzleDelegate.view];
		[myPuzzleDelegate initWithParent:self];
		myPuzzleDelegate.view.alpha = 0.0;
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
		[UIView setAnimationDelegate:self];
		[UIView setAnimationDidStopSelector:@selector(puzzlesFadedIn:finished:context:)];
		[UIView setAnimationDuration:0.5];
		myPuzzleDelegate.view.alpha = 1.0;
		[UIView commitAnimations];
	}
}

-(void)puzzlesFadedIn:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
	//
}

Objective C 添加到OS X状态托盘

@interface Tray : NSObject <NSApplicationDelegate> {
	NSStatusItem *trayItem;
}
@end

@implementation Tray

- (IBAction)testAction:(id)sender;
{
	NSLog(@"Hello World");
}

- (IBAction)quitAction:(id)sender;
{
	[NSApp terminate:sender];
}

- (void)applicationDidFinishLaunching:(NSNotification *)note;
{
	NSZone *zone = [NSMenu menuZone];
	NSMenu *menu = [[[NSMenu allocWithZone:zone] init] autorelease];
	NSMenuItem *item;
	
	item = [menu addItemWithTitle:@"Testing" action:@selector(testAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	item = [menu addItemWithTitle:@"Quit" action:@selector(quitAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	trayItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
	[trayItem setMenu:menu];
	[trayItem setHighlightMode:YES];
	[trayItem setTitle:@"HERE"];
}

- (void)dealloc;
{
	[trayItem release];
	[super dealloc];
}

@end

Objective C CGGradient2

CGContextRef ctx = UIGraphicsGetCurrentContext();
 
 CGContextSaveGState(ctx);
 CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
 CGFloat colors [] = {		
 0.7,0.0,0.0,1.0,
 1.0,0.0,0.0,1.0
 } ;
 
 CGFloat locations[] = {0.5,0.0};
 CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, colors, locations, 2);
 CGColorSpaceRelease(colorspace);
 CGContextAddRect(ctx, rect);
 CGContextDrawLinearGradient(ctx, gradient, CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)),
 CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)),0);
 CGGradientRelease(gradient);
 CGContextRestoreGState(ctx);

Objective C Cocos2d滚动背景

//Create our scrolling background
- (void)setupBackgroundImage
{
    //create both sprite to handle background
    background1 = [CCSprite spriteWithFile:@"cloud_bg1.png"];
    background2 = [CCSprite spriteWithFile:@"cloud_bg2.png"];
    
    background1.position = ccp(0, size.height/2);
    background2.position = ccp(size.width, size.height/2);
    
    //add them to main layer
    [self addChild:background1 z:0];
    [self addChild:background2 z:0];
    
    //add schedule to move backgrounds
    [self schedule:@selector(scroll:)];
}

- (void)scroll:(ccTime)dt {
    
    background1.position = ccp( background1.position.x - 2, background1.position.y );
    background2.position = ccp( background2.position.x - 2, background2.position.y );
    
    //reset position when they are off from view.
    if (background1.position.x == - (size.width/2)) {
        background1.position = ccp(size.width+(size.width/2), size.height/2);
    }
    else if (background2.position.x == - (size.width/2)) {
        background2.position = ccp(size.width+(size.width/2), size.height/2);
    }
    
}

Objective C iPhone iPad

-�void� viewDidLoad {


    #if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)

    NSString *str;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        str = [NSString stringWithString:@"Running as an iPad application"];
    } else {
        str = [NSString stringWithString:
                  @"Running as an iPhone/iPod touch application"];
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Platform"
                                                    message:str
                                                   delegate:nil
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];     

    #endif    
    
    [super viewDidLoad];
}

Objective C 获取月份名称

- (NSString*)getMonthName:(int)month
{
	NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	return [[formatter monthSymbols] objectAtIndex:(month - 1)];
}

Objective C 如何制作圆角图像

void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight);
{
	float fw, fh;
	if (ovalWidth == 0 || ovalHeight == 0) {
		CGContextAddRect(context, rect);
		return;
	}
	CGContextSaveGState(context);
	CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
	CGContextScaleCTM (context, ovalWidth, ovalHeight);
	fw = CGRectGetWidth (rect) / ovalWidth;
	fh = CGRectGetHeight (rect) / ovalHeight;
	CGContextMoveToPoint(context, fw, fh/2);
	CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
	CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
	CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
	CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
	CGContextClosePath(context);
	CGContextRestoreGState(context);
}

- (UIImage *)roundCornersOfImage:(UIImage *)source;
{
	int w = source.size.width;
	int h = source.size.height;
	
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
	
	CGContextBeginPath(context);
	CGRect rect = CGRectMake(0, 0, w, h);
	addRoundedRectToPath(context, rect, 5, 5);
	CGContextClosePath(context);
	CGContextClip(context);
	
	CGContextDrawImage(context, CGRectMake(0, 0, w, h), source.CGImage);
	
	CGImageRef imageMasked = CGBitmapContextCreateImage(context);
	CGContextRelease(context);
	CGColorSpaceRelease(colorSpace);
	
	return [UIImage imageWithCGImage:imageMasked];    
}

Objective C 获取本地电话时间

- (NSDate*)getLocalTime:(NSDate*)sourceDate
{
	NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
	
	NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
	NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
	NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
	
	NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
	return destinationDate;
}

Objective C 如何找到两个CG点之间的距离?

CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));