



UsingBlocksAsContextInfo - Demonstrates how to implement Objective-C "blocks" passed in as the 'contextInfo' to NSAlert, helping to handle the alert result.
// This is a quick tip on how to use the context info as a block parameter.
// You can use this type of pattern for any methods that have a delegate/selector/contextInfo pattern.
- (void)btnShowAlertClicked:(id)sender {
NSAlert *alert = [NSAlert alertWithMessageText:@"Alert Message"
defaultButton:@"Default Button"
alternateButton:@"Alternate Button"
otherButton:@"Other Button"
informativeTextWithFormat:@"Informative Text"];
BOOL someLocalVariable = YES;
// We create a block that can easily access local variables to this method.
// This is much easier than trying to package them all up into a contextInfo object
void (^blockCallback)(NSInteger) = ^(NSInteger returnCode) {
// Inside the block callback we can easily access locals
if (someLocalVariable) {
if (returnCode == NSAlertDefaultReturn) {
[button setTitle:@"Default Return Button Clicked!"];
} else {
[button setTitle:@"Something else clicked...try again."];
}
}
};
// We copy the block, since it needs to stay alive for longer than the current scope
[alert beginSheetModalForWindow:self.window
modalDelegate:[self class]
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:Block_copy(blockCallback)];
}
+ (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void (^)(NSInteger returnCode))continuationHandler {
continuationHandler(returnCode);
// The block must always be retained before the first call. This is the matching release
Block_release(continuationHandler);
}
static void _processData(AppDelegate *self, NSInteger i) {
// Notice that the _window can be accessed here, even though it is a private ivar
// A compiler error (or warning) will happen if this method was outside the @implementation scope of AppDelegate.
NSLog(@"Processing %d in window %@", i, self->_window);
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
for (NSInteger i = 0; i < 200; i++) {
_processData(self, i);
}
}
#import <Cocoa/Cocoa.h> |
int main(int argc, char *argv[]) |
{ |
return NSApplicationMain(argc, (const char **) argv); |
} |
NSMainNibFile,并load之程序启动完成
程序状态管理和维护通过
NSApplication,NSApplicationDelegateNSNibLoading,单独load
使用NSBundle 3个方法
+ (BOOL)loadNibFile:(NSString *)fileName externalNameTable:(NSDictionary *)context withZone:(NSZone *)zone;
+ (BOOL)loadNibNamed:(NSString *)nibName owner:(id)owner;
- (BOOL)loadNibFile:(NSString *)fileName externalNameTable:(NSDictionary *)context withZone:(NSZone *)zone;
看起来第二个更简单些
另外Document base Application NSDocument 类可以使用
- (NSString *)windowNibName;方法加载目标Nib文件
NSWindowController 同样可以使用上面的这个方法

NSApplicationDelegate
实现
- (NSMenu *)applicationDockMenu:(NSApplication *)sender
{
return _menu;
}
2 状态条菜单
需要NSMenuDelegate protocol
@protocol NSMenuDelegate <NSObject>
@optional
- (void)menuNeedsUpdate:(NSMenu*)menu;
- (NSInteger)numberOfItemsInMenu:(NSMenu*)menu;
- (BOOL)menu:(NSMenu*)menu updateItem:(NSMenuItem*)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel;
- (void)menuWillOpen:(NSMenu *)menu;
- (void)menuDidClose:(NSMenu *)menu;
- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
@end

给StatusBar加Icon
- (void)activateStatusMenu:(int)status
{
NSStatusBar *bar = [NSStatusBar systemStatusBar];
icon = [bar statusItemWithLength:NSVariableStatusItemLength];
[icon retain];
NSString* filePath;
switch (status) {
case 0:
filePath = [[NSBundle mainBundle] pathForResource:@"sync_complete" ofType:@"png"];
break;
case 1:
filePath = [[NSBundle mainBundle] pathForResource:@"sync_processing" ofType:@"png"];
break;
case 2:
filePath = [[NSBundle mainBundle] pathForResource:@"sync_error" ofType:@"png"];
break;
default:
break;
}
NSImage* image = [[NSImage alloc] initWithContentsOfFile:filePath];
[icon setImage:image];
[icon setHighlightMode:YES];
[icon setMenu:mainMenu];
}
关于菜单就讲这么多
NSWindowController
- (id)initWithWindow:(NSWindow *)window
或者使用 - (NSString *)windowNibName
NSWindowController *controller = [[c alloc] init];
if (_windowControllers == nil) {
_windowControllers = [NSMutableArray new];
}
[_windowControllers addObject:controller];
[controller showWindow:self];//显示Window,self 为NSWindowController
[controller release];
注册窗口关闭通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_windowClosed:) name:NSWindowWillCloseNotification object:nil];
处理关闭窗口通知,移出不需要的Window
- (void)_windowClosed:(NSNotification *)note {
NSWindow *window = [note object];
for (NSWindowController *winController in _windowControllers) {
if (winController.window == window) {
[[winController retain] autorelease]; // Keeps the instance alive a little longer so things can unbind from it
[_windowControllers removeObject:winController];
break;
}
}
}
#pragma mark KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog(@"dict= %@ obj= %@,keypath=%@",change,object,keyPath);
// if (object ==xxx) {
// if ([keyPath isEqualToString:@"title"]) {
//
// }else if ([keyPath isEqualToString:@"name"]) {
//
// }
// } else if(object == bbb){
//
// }
if (context == 123) {
} else if(context == 456){
}
}
-(void)traceObj:(Entry*)obj
{
[obj addObserver:self
forKeyPath:@"title"
options:0
context:123];
[obj addObserver:self
forKeyPath:@"name"
options:0
context:456];
[objB addObserver:self
forKeyPath:@"name"
options:0
context:NULL];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)viewDidLoad
{
[super viewDidLoad];
Entry *entry =[[Entry alloc] init];
[self traceObj:entry];
[entry setValue:@"abc" forKey:@"title"];
id msg=[entry valueForKey:@"titl"];
NSLog(@"%@",msg);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardShow:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textChanged:)
name:UITextFieldTextDidChangeNotification
object:nil];
}
-(void)textChanged:(NSNotification*)noti
{
NSLog(@"%@ %@",input.text,[noti userInfo]);
NSString *sTr =input.text;
if ([sTr length] >=10) {
input.text = [sTr substringWithRange:NSMakeRange(0,9)];
UIAlertView *alert=[[[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"2",@"cancel",nil] autorelease];
[alert show];
}
NSMutableArray *array=[[NSMutableArray alloc] initWithCapacity:10];
}
-(void)keyboardShow:(NSNotification*)noti
{
if ([[noti name] isEqualToString:UIKeyboardWillShowNotification]) {
input.center = CGPointMake(input.center.x, input.center.y-200);
} else {
input.center = CGPointMake(input.center.x, input.center.y+200);
}
}
######################################
@implementation Entry
@synthesize title;
@synthesize price;
@synthesize image53;
@synthesize image;
-(id)valueForUndefinedKey:(NSString *)key
{
return [NSString stringWithFormat:@"%@ not have value for key %@",self,key];
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
#!/bin/sh
apppath=`xcodebuild |grep Validation|awk '{print $2}'`
sign='iPhone Distribution:Yarshure Kong Healthcare Communications Co., Ltd.'
ipapath=`pwd`'/BlanceBall.ipa'
echo $apppath
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/PackageApplication -s "$sign" "$apppath" -o "$ipapath"
scp "$ipapath" macgeeks:~/macgeeks.cn/bbs/apps/
NSString *path=[[NSBundle mainBundle] pathForResource:@"111.jpg" ofType:nil];
CIImage *image=[CIImage imageWithData:UIImagePNGRepresentation(imageView.image)];//[CIImage imageWithContentsOfURL:[NSURL fileURLWithPath:path]];
NSDictionary *pro=[image properties];
NSLog(@"-- %@",pro);
CIFilter *filter = [CIFilter filterWithName:@"CIAffineTransform"];//CIBloom
[filter setDefaults];
CGAffineTransform a=CGAffineTransformMakeRotation(0.5);
[filter setValue:[NSValue valueWithCGAffineTransform:a] forKey:@"inputTransform"];
// [filter setValue:[NSNumber numberWithFloat:0.5] forKey:@"inputRadius"];
[filter setValue: image forKey: @"inputImage"];
CIImage *result = [filter valueForKey: @"outputImage"];
CIContext *context =[CIContext contextWithOptions:nil];
CGImageRef cgimg=[context createCGImage:result fromRect:[result extent]];
imageView.image = [UIImage imageWithCGImage:cgimg scale:1.0 orientation:UIImageOrientationUp];
sample.zippic.animationDuration=10;2个动画一起,而且时间不一样animation.duration = 2;可能10的这个根本没动起来我写了个demo ,你可以参考下,看看是否满足你的要求
在 2011-7-18,下午6:01, ぃ空空一筑か 写道:孔老师,您好,最近做项目需要用到图片自动切换,按照我自己的想法,是遍历文件目录下的图片,随机取出一副,然后随机用一个动画效果,用UIView的beginAnimation和commitAnimation来实现。之前看过您的PPT,上面恰好有个UIImageView的动画,将图片当作一个集合给ImageView的animationImages属性,然后设置好其它的参数,再startAnimating和stopAnimation来实现。这种方法用于自动切换图片应该是最简单的,但我想在图片切换中加动画,查找了UIImageView的所有属性和方法,没有能实现的。也试过了用UIView的Layer来加载CATranition,但是没有效果。想问您一下,用UIImageView能不能实现我想要的?如果能的话,请指点一下,谢谢!下面是我相关的代码NSArray *fileList=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:DOCUMENTS_FOLDER error:nil];picNames=[[NSMutableArray alloc] initWithArray:[fileList pathsMatchingExtensions:[NSArray arrayWithObject:@"jpg"]]];imageArray=[[NSMutableArray alloc] initWithCapacity:[picNames count]];for (int i=0; i<[picNames count]; i++) {NSString *sPath=[NSString stringWithFormat:@"%@/%@",DOCUMENTS_FOLDER,[picNames objectAtIndex:i]];[imageArray addObject:[UIImage imageWithContentsOfFile:sPath]];}pic=[[UIImageView alloc] init];NSArray arrayWithArray:imageArray];pic.animationDuration=10;pic.animationRepeatCount=-1;CATransition *animation = [CATransition animation];animation.duration = 2;animation.timingFunction = UIViewAnimationCurveEaseInOut;animation.fillMode = kCAFillModeForwards;animation.removedOnCompletion = YES;animation.type = @"cube";[pic.layer addAnimation:animation forKey:@"animation"];[pic startAnimating];
错误不太应该,应该只是慢,感觉卡的样子。
建议改成iPod那个样子,不在屏幕上的图片不要加载到内存,用张default代替,需要的时候再用真正的渲染。
苹果是这么玩的,肯定会有提升的。
孔祥波
在 2011-9-9,下午4:06, ぃ空空一筑か 写道:
孔老师你好,那个开源的coverFlow例子,你应该很早就看过,我想问个问题,我们项目里面前段时间用到了这个东西,解决了大多数问题,但还有个问题解决不了。
我们的项目是在Ipad上面,滑动的图片比较大,在滑动时,如果手指滑动的时间快于动画的时间,连续这样,就会出现图片绘制错位的情况,在模拟器上面不存在这种表况,我估计是跟设备有关系,处理器不够好,不知道这个有没有解决的办法。然后就是在Ipad1上面滑动的流畅度远不如在Ipad2上面的,已经试过一些优化的方法了,不知道有没有什么方法可以让图片滑动更加流畅点。
谢谢!









Recent Comments