iOS中一些程式碼

走路放慢腳步發表於2014-03-30

判斷Iphone5

#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : 0)

隱藏鍵盤,可以呼叫textfield的endediting事件

[[UIApplication sharedApplication].keyWindow endEditing:YES];

NSNotification

釋出監聽

[[NSNotificationCenter defaultCenter] postNotificationName:[NSString]
                                                    object:[NSObject]];

監聽事件

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(:)
                                                 name:[NSString]
                                               object:nil];

移除監聽

[[NSNotificationCenter defaultCenter] removeObserver:self
                    name:notifi_selected_city
                                      object:nil];

獲取notification中附帶的object

NSDictionary *Dic = [notification object];

關於繼承

SuperClass中的代理方法,在SonClass中重寫,SonClassController的代理方法會覆蓋SuperClass的代理方法。需要用[Super Function]來呼叫。

字型陰影設定

button.titleLabel.shadowOffset = CGSizeMake(0, 1);
button.titleLabel.shadowColor = [UIColor blackColor];
button.titleLabel.layer.shadowOpacity = 0.3f;

畫面漸淡消失

-(void)closeView
{
    [UIView animateWithDuration:1.5 animations:^{
        self.alpha = 0;
        self.transform = CGAffineTransformMakeScale(1.2, 1.2);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

獲取App版本號

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

代理的實現

@class delegateClass;
@protocol protocolName <NSObject>
     - (void)image;
@end
@property(assign,nonatomic) id<protocolName> delegate;

從Storyboard獲取view

 UIStoryboard* story = [UIStoryboard storyboardWithName:@"main" bundle:nil];
 className* class = (className*)[story instantiateViewControllerWithIdentifier:@"className"];

關於NSTimer

NSTimer
暫停
[countTime setFireDate:[NSDate distantFuture]];
繼續
[countTime setFireDate:[NSDate date]];

UIImagePickerController類使用

//跳轉到照相介面
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:imagePicker animated:YES completion:nil];

//代理方法,獲得image
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
    image = [info objectForKey:UIImagePickerControllerOriginalImage]
} else {
    image = [info objectForKey:UIImagePickerControllerEditedImage];
}

NavigationController的按鈕

UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
self.navigationItem.rightBarButtonItem = rightItem;

計算字串的高度和長度

//長度
CGSize sizeName = [strText sizeWithFont:theFont
                              constrainedToSize:CGSizeMake(MAXFLOAT, 0.0)
                                  lineBreakMode:NSLineBreakByWordWrapping];
//高度
CGSize sizeName = [strText sizeWithFont:theFont
                              constrainedToSize:CGSizeMake(100.0, MAXFLOAT)
                                  lineBreakMode:NSLineBreakByWordWrapping];

拉伸UIImage

講圖片的畫素拉到和UIButton一樣,UIEdgeInsetsMake()做成的矩形,用來填充不夠的畫素。

[b setBackgroundImage:[[UIImage imageNamed:selectedName] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 10, 0)]
                         forState:UIControlStateSelected];

設定vidw的layer

需要設定view.layer.clipsToBounds = YES,設定subview的cornerRadius
StackOverflow上看到的:

Because shadow is an effect done outside the View, and that masksToBounds set to YES will tell the UIView not to draw everything that is outside itself.
If you want a roundedCorner view with shadow I suggest you do it with 2 views:

UIView *view1 = [[UIView alloc] init];
UIView *view2 = [[UIView alloc] init];

view1.layer.cornerRadius = 5.0;
view1.layer.masksToBounds = YES;
view2.layer.cornerRadius = 5.0;
view2.layer.shadowColor = [[UIColor blackColor] CGColor];
view2.layer.shadowOpacity = 1.0;
view2.layer.shadowRadius = 10.0;
view2.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
[view2 addSubview:view1];
[view1 release];

獲取截圖

+ (UIImage *)screenshot
{
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;

    if (NULL != UIGraphicsBeginImageContextWithOptions) {
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    } else {
        UIGraphicsBeginImageContext(imageSize);
    }

    CGContextRef context = UIGraphicsGetCurrentContext();

    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
            CGContextSaveGState(context);

            CGContextTranslateCTM(context, [window center].x, [window center].y);

            CGContextConcatCTM(context, [window transform]);

            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            [[window layer] renderInContext:context];

            CGContextRestoreGState(context);
        }
    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}
//壓縮影像
NSData *imageData = UIImageJPEGRepresentation(screenshot, .0001);

相關文章