iOS中一些代码

判断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 = 
} else { image =
; }

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);
    原文作者:走路放慢脚步
    原文地址: https://segmentfault.com/a/1190000000450766
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞