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 如何制作圆角图像

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 使用图案图像绘制背景的正确方法

// Save the current graphics state first so we can restore it.
[[NSGraphicsContext currentContext] saveGraphicsState];

// Change the pattern phase.
[[NSGraphicsContext currentContext] setPatternPhase:
    NSMakePoint(0,[self frame].size.height)];

// Stick the image in a color and fill the view with that color.
NSImage *anImage = [NSImage imageNamed:@"bricks"];
[[NSColor colorWithPatternImage:anImage] set];
NSRectFill([self bounds]);

// Restore the original graphics state.
[[NSGraphicsContext currentContext] restoreGraphicsState];

Objective C 使用UIScrollView滚动,平移,缩放

Scrolling - A scroll view acts as a container for a larger subview, allowing you to pan around the subview by touching the screen. Vertical and horizontal scroll bars indicate the position in the subview.

-(void) viewDidLoad 
{
    [super viewDidLoad];

    // Set the frame that is twice the size of the screen
    CGRect scrollFrame = CGRectMake(20, 90, 280, 280);
	
	// In this example, the UIImage size is greater than the scrollFrame size
	UIImage *bigImage = [UIImage imageNamed:@"appleLogo.jpg"];
	UIImageView *largeImageView = [[UIImageView alloc] initWithImage:bigImage];
   
   
    // Create the UIScrollView to have the size of the window, matching its size
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
	
	[scrollView addSubview:largeImageView]; 
	
	// Tell the scrollView how big its subview is
    scrollView.contentSize = largeImageView.frame.size;   // Important
   
    [self.view addSubview:scrollView];
}	

- You can hide these scroll bars using the showsHorizontalScrollIndicator and showsVerticalScrollIndicator properties
- If you play around with the previous code, you’ll notice that if you scroll quickly to the edge of the subview, the scroll view actually moves a little too far before springing back. This behavior is controlled by the bounce property. You can restrict bouncing to the x- or y-axis using the alwaysBounceHorizontal and alwaysBounceVertical properties, or you can disable it entirely by setting bounce to NO.



Paging 

Scroll views support the paging of their content—the ability to add multiple subviews as “pages” and then scroll between them as you might turn the pages of a book. 


-(void) viewDidLoad 
{
    [super viewDidLoad];

   
    // Create the UIScrollView to have the size of the view, matching its size
    CGRect screenRect = [[self view] bounds];
	
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
	[scrollView setPagingEnabled:YES];
	[[self view] addSubview:scrollView];

    // Create the page with a frame that is twice the size of the screen
    CGRect bigRect = screenRect;
	bigRect.size.width *= 2.0;
    HypnosisView *view = [[HypnosisView alloc] initWithFrame:screenRect];
	
	// Move the rectangle for the other HypnosisView to the right, just off
	// the screen
	screeRect.origin.x = screenRect.size.width;
	HypnosisView *anotherView = [[HypnosisView alloc] initWithFrame:screenRect];
    [scrollView addSubview:view];
	    
    // Tell the scrollView how big its subview is
    [scrollView setContentSize:bigRect.size];

}


Zoom

You can also zoom in and out of an image using a scroll view. The minimumZoomScale and maximumZoomScale properties control the scale by which you can zoom in and out. By default, both of these properties are set to the same value (1.0), which disables zooming. You must implement one of the UIScrollViewDelegate methods to return the view that is being zoomed.

Delegate:           <UIScrollViewDelegate>
Instance variable: 	UIImageView *largeImageView


-(void) viewDidLoad 
{
    [super viewDidLoad];

    // Set the frame that is twice the size of the screen
    CGRect scrollFrame = CGRectMake(20, 90, 280, 280);
	
	// Create the UIScrollView to have the size of the window, matching its size
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
	scrollView.minimumZoomScale = 0.5;
	scrollView.maximumZoomScale = 2.0;
	scrollView.delegate = self;
	
	// In this example, the UIImage size is greater than the scrollFrame size
	UIImage *bigImage = [UIImage imageNamed:@"appleLogo.jpg"];
	largeImageView = [[UIImageView alloc] initWithImage:bigImage];
	
	// Tell the scrollView how big its subview is
    scrollView.contentSize = largeImageView.frame.size;   // Important

    [scrollView addSubview:largeImageView]; 
   
    [self.view addSubview:scrollView];
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return largeImageView;
}

Objective C 进展和活动指标

// Showing progress 

NSTimer *timer;

- (void)updateProgress:(NSTimer *)sender
{
	UIProgressView *progress = [sender userInfo];
	
	//have we completed?
	if (progress.progress == 1.0)
		[timer invalidate];
	else
		progress.progress += 0.05;
}

- (void)viewDidLoad {
	
    [super viewDidLoad];
	
	UIProgressView *myProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
	
	CGRect progressFrame = CGRectMake(10,100,300,25);
	[myProgressView setFrame:progressFrame];
	
	[myProgressView setProgress:0.0];
	
	[self.view addSubview:myProgressView];
	
	//create timer
	timer = [[NSTimer scheduledTimerWithTimeInterval:0.1 
											  target:self 
											selector:@selector(updateProgress:) 
											userInfo:myProgressView 
											 repeats:YES] retain];
}


// Showing Activity 


- (void)viewDidLoad {
	
    [super viewDidLoad];
	
	[self.view setBackgroundColor:[UIColor blackColor]];
	
	UIActivityIndicatorView *myActivityView = [[UIActivityIndicatorView alloc] 
											   initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
	
	CGRect activityFrame = CGRectMake(130,100,50,50);
	[myActivityView setFrame:activityFrame];
	myActivityView.hidesWhenStopped = YES;	
	[myActivityView startAnimating];
	
	[self.view addSubview:myActivityView];
	
}

Objective C NSDate到NSString

NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date] 
                                                      dateStyle:NSDateFormatterShortStyle 
                                                      timeStyle:NSDateFormatterFullStyle];
NSLog(@"%@",dateString);

Objective C xcode用参数声明和调用方法

//declare the method
+(void) goGameScene:(int)lvl;   //SceneManager.h
+(void)goGameScene:(int)lvl {   //SceneManager.m

//to call the method
#import "SceneManager.h"        //in the .h of class you call it from
[SceneManager goGameScene:lvl]; //in the .m of the class you call it from

Objective C xcode Objective C - 构建类/对象

//----------------------------------
//  Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject {
    int age;
    int weight;
}
-(void) print;
-(void) setAge: (int) a;
-(void) setWeight: (int) w;
@end



//----------------------------------
//  Person.m
//  codeTester

#import "Person.h"

@implementation Person
-(void) print{
    NSLog(@"I am %i years old and weight %i pounds", age, weight);
}
-(void) setAge: (int) a{
    age=a;
}
-(void) setWeight: (int) w{
    weight=w;
}
@end



//----------------------------------
//how to call

#import "Person.h"      //dont forget to import it

Person *bucky;
bucky = [Person alloc]; //give it memory
bucky = [bucky init];   //allows us to defualt init metod
[bucky setAge:23];
[bucky setWeight:350];
[bucky print];

Objective C 你自己的NSDateFormatter

NSDate *today = [[NSDate alloc] init];
NSDateFormatter *forIdPeriodFormatterStyle = [[NSDateFormatter alloc] init];[forIdPeriodFormatterStyle setDateFormat:@"yyyyMMdd"];//you can add HH and mm 
NSString *idPeriodString = [forIdPeriodFormatterStyle stringFromDate:today];

Objective C NSString到NSNumber

NSNumber *idPeriod = [NSNumber numberWithInt:[idPeriodString intValue]];