·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> IOS开发基础知识--碎片19

IOS开发基础知识--碎片19

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

 

1:键盘事件顺序

UIKeyboardWillShowNotification          // 键盘显示之前
UIKeyboardDidShowNotification           // 键盘显示完成后
UIKeyboardWillHideNotification          // 键盘隐藏之前
UIKeyboardDidHideNotification           // 键盘消息之后
UIKeyboardWillChangeFrameNotification   // 键盘大小改变之前
UIKeyboardDidChangeFrameNotification    // 键盘大小改变之后

 

2:程序报-[__NSCFDictionary xxx]: unrecognized selector sen

原因就是对象是一个字典,所以不能用点语法,postList.slots错误

解决办法:[postList valueForKey:@"slots"],使用这种语法  valueForKey  就可以了。


3:UIScreen学习记录

UIScreen对象包含了整个屏幕的边界矩形。当构造应用的用户界面接口时,你应该使用该对象的属性来获得推荐的矩形大小,用以构造你的程序窗口。

CGRect bound = [[UIScreen mainScreen] bounds];  // 返回的是带有状态栏的Rect  
CGRect frame = [[UIScreen mainScreen] applicationFrame];  // 返回的是不带有状态栏的Rect  
float scale = [[UIScreen mainScreen] scale]; // 得到设备的自然分辨率  

对于scale属性需要做进一步的说明: 

以前的iphone 设备屏幕分辨率都是320*480,后来apple 在iPhone 4中采用了名为Retina的显示技术,iPhone 4采用了960x640像素分辨率的显示屏幕。由于屏幕大小没有变,还是3.5英寸,分辨率的提升将iPhone 4的显示分辨率提升至iPhone 3GS的四倍,每英寸的面积里有326个像素。

scale属性的值有两个:
scale = 1; 的时候是代表当前设备是320*480的分辨率(就是iphone4之前的设备)
scale = 2; 的时候是代表分辨率为640*960的分辨率

// 判断屏幕类型,普通还是视网膜  
    float scale = [[UIScreen mainScreen] scale];  
    if (scale == 1) {  
        bIsRetina = NO;  
        NSLog(@"普通屏幕");  
    }else if (scale == 2) {  
        bIsRetina = YES;  
        NSLog(@"视网膜屏幕");  
    }else{  
        NSLog(@"unknow screen mode !");  
    }  

 

4:IOS开发NSBundle对象使用详解

bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.

我们的程序是一个bundle. 在Finder中,一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle

bundle中的有些资源可以本地化.例如,对于foo.nib,我们可以有两个版本: 一个针对英语用户,一个针对法语用户. 在bundle中就会有两个子目录:English.lPRoj和French.lproj,我们把各自版本的foo.nib文件放到其中. 当程序需要加载foo.nib文件时,bundle会自动根据所设置的语言来加载.通过使用下面的方法得到程序的main bundle
NSBundle *myBundle = [NSBundle mainBundle];
一般我们通过这种方法来得到bundle.如果你需要其他目录的资源,可以指定路径来取得bundle
NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];
一旦我们有了NSBundle 对象,那么就可以访问其中的资源了
// Extension is optional
NSString *path = [goodBundle pathForImageResource:@"Mom"];
NSImage *momPhoto = [[NSImage alloc] initWithContentsOfFile:path];
bundle中可以包含一个库. 如果我们从库得到一个class, bundle会连接库,并查找该类:
Class newClass = [goodBundle classNamed:@"Rover"];
id newInstance = [[newClass alloc] init];
如果不知到class名,也可以通过查找主要类来取得
Class aClass = [goodBundle principalClass];
id anInstance = [[aClass alloc] init];
可以看到, NSBundle有很多的用途.在这章中, NSBundle负责(在后台)加载nib文件. 我们也可以不通过NSWindowController来加载nib文件, 直接使用NSBundle:
BOOL successful = [NSBundle loadNibNamed:@"About" owner:someObject];
注意噢, 我们指定了一个对象someObject作为nib的File”s Owner

获取xml文件
NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath]; 

获取属性列表
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ViewControllers" ofType:@"plist"]]; 

 

5:单位换算,PX换算成磅

用ps设计ios App字体以像素为单位,而ios开发人员写代码的时候是以磅为单位,请问像素与磅之间的换算

30px转成磅为单位=22磅=二号磅=(像素/96)*72    =(30/96)*72    =22.5磅

中文字号VS英文字号(磅)VS像素值的对应关系:
八号=5磅(5pt) ==(5/72)*96=6.67 =6px(像素)
七号=5.5磅 ==(5.5/72)*96=7.3 =7px(像素)
小六=6.5磅 ==(6.5/72)*96=8.67 =8px(像素)
六号=7.5磅 ==(7.5/72)*96=10px(像素)
小五=9磅 ==(9/72)*96=12px(像素)
五号=10.5磅 ==(10.5/72)*96=14px(像素)
小四=12磅 ==(12/72)*96=16px(像素)
四号=14磅 ==(14/72)*96=18.67 =18px(像素)
小三=15磅 ==(15/72)*96=20px(像素)
三号=16磅 ==(16/72)*96=21.3 =21px(像素)
小二=18磅 ==(18/72)*96=24px(像素)
二号=22磅 ==(22/72)*96=29.3 =29px(像素)
小一=24磅 ==(24/72)*96=32px(像素)
一号=26磅 ==(26/72)*96=34.67 =34px(像素)

 

6:UIButton一些细节问题

// 能够定义的button类型有以下6种,
// typedef enum {
// UIButtonTypeCustom = 0, 自定义风格
// UIButtonTypeRoundedRect, 圆角矩形 
// UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
// UIButtonTypeInfoLight, 亮色感叹号
// UIButtonTypeInfoDark, 暗色感叹号
// UIButtonTypeContactAdd, 十字加号按钮
// } UIButtonType;

/* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
//以下是几种状态
// enum {
// UIControlStateNormal = 0, 常规状态显现 
// UIControlStateHighlighted = 1 << 0, 高亮状态显现 
// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
// UIControlStateSelected = 1 << 2, 选中状态 
// UIControlStateApplication = 0x00FF0000, 当应用程序标志时 
// UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他 
// };
/*
* 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
* 那么可以去掉这个功能
*/
button1.adjustsImageWhenHighlighted = NO;
/*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
button1.adjustsImageWhenDisabled = NO;
/* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
button1.showsTouchWhenHighlighted = YES;


长按事件实例:


UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [aBtn setFrame:CGRectMake(40, 100, 60, 60)];
    [aBtn setBackgroundImage:[UIImage imageNamed:@"111.png"]forState:UIControlStateNormal];
    //button点击事件
    [aBtn addTarget:self action:@selector(btnShort:)forControlEvents:UIControlEventTouchUpInside];
    //button长按事件
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(btnLong:)]; 
    longPress.minimumPressDuration = 0.8; //定义按的时间
    [aBtn addGestureRecognizer:longPress];

-(void)btnLong:(UILongPressGestureRecognizer*)gestureRecognizer{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        NSLog(@"长按事件");
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"消息" message:@"确定删除该模式吗?" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
        [alert show];
    }
}

 

7:UIApplication知识点

UIApplication对象,这个对象在iOS中是一个单例,我们通过[UIApplication sharedApplication]获得,

设置显示消息数,显示在应用程序图标右上角,[UIApplication sharedApplication].applicationIconBadgeNumber=9;可以通过获得NSIntger x=[UIApplication sharedApplication].applicationIconBadgeNumber; 

防止屏幕睡眠:[UIApplication sharedApplication].idleTimerDisabled=YES; 

设置状态栏样式在app delegate中:[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

 

8:一个倒计时的功能代码

#import "ViewController.h"

@interface ViewController ()
{
    NSTimer     *timer;
    NSInteger   nowSeconds;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    nowSeconds = 30 * 100;  //(定义的30秒进行倒计)
    timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)timerAction
{
    if(nowSeconds<0)
    {
        [timer invalidate];
        timer = nil;
        return;
    }
    nowSeconds--;
    if(nowSeconds<=0)
    {
        self.mylable.text = @"正在揭晓...";
        return;
    }
    int m = (int)nowSeconds / 6000;
    int s = (int)(nowSeconds/100) - m*60;
    NSString* f1 = s > 9 ? [NSString stringWithFormat:@"%d",s] : [@"0" stringByAppendingFormat:@"%d",s];
    int ms = nowSeconds % 100;
    NSString* f2 = ms > 9 ? [NSString stringWithFormat:@"%d",ms] : [@"0" stringByAppendingFormat:@"%d",ms];
    self.mylable.text = [NSString stringWithFormat:@"0%d:%@:%@",m,f1,f2];
}
@end

 

9:BlocksKit插件运用

引入#import <BlocksKit/BlocksKit.h>
#import <BlocksKit/BlocksKit+UIKit.h>

常见的一些blocks:

    //视图
    UIView *bcView=[[UIView alloc]init];
    bcView.backgroundColor=[UIColor redColor];
    bcView.frame=CGRectMake(30, 40, 50, 20);
    [bcView bk_whenTapped:^{
        NSLog(@"单击响应");
    }];
    [bcView bk_whenDoubleTapped:^{
        NSLog(@"双击响应");
    }];
    [bcView bk_whenTouches:1 tapped:3 handler:^{
        NSLog(@"三击响应");
    }];
    [self.view addSubview:bcView];
    
    
    //Control
    btn=[[UIButton alloc]initWithFrame:CGRectMake(70, 70, 50, 50)];
    [btn setTitle:@"Save" forState:UIControlStateNormal];
    btn.backgroundColor=[UIColor grayColor];
    [btn bk_addEventHandler:^(id sender) {
        NSLog(@"UIControlEventTouchUpInside响应");
        //判断跟移除响应
        [self getUIButtonEventHandler];
    } forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    
    //UIAlertView
    UIAlertView *alertView=[UIAlertView bk_showAlertViewWithTitle:@"弹出窗效果" message:@"你好,请选择" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定",@"不想选"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex==0) {
            NSLog(@"你选择第一个");
        }
        else if(buttonIndex==1)
        {
            NSLog(@"你选择第二个");
        }
        else
        {
            NSLog(@"选择其它个");
        }
    }];
    [alertView show];
    
    
    __block NSInteger total=0;
    UIAlertView *otherAlertView=[[UIAlertView alloc]bk_initWithTitle:@"动态增加" message:@"是否是要增加"];
    NSInteger index1 = [otherAlertView bk_addButtonWithTitle:@"确定" handler:^{ total++;
        NSLog(@"%ld",total);
    }];
    [otherAlertView.bk_dynamicDelegate alertView:otherAlertView clickedButtonAtIndex:index1];
    [otherAlertView show];
    
    
    UIAlertView *showAlert=[[UIAlertView alloc] initWithTitle:@"系统自带的alertview" message:@"显示信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];
    showAlert.bk_cancelBlock=^()
    {
        NSLog(@"取消响应");
    };
    showAlert.bk_willShowBlock = ^(UIAlertView *view) { NSLog(@"显示之前响应");};
    showAlert.bk_didShowBlock = ^(UIAlertView *view) { NSLog(@"显示出弹出窗响应"); };
    [showAlert show];
    
    //UIActionSheet
    UIActionSheet *myactionSheet=[[UIActionSheet alloc]initWithTitle:@"显示信息" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相关内容的显示" otherButtonTitles:nil];
    myactionSheet.bk_cancelBlock=^()
    {
        NSLog(@"选择取消actionSheet");
    };
    [myactionSheet showInView:self.view];
    
    
    //UITextField
    UITextField *myTextField = [[UITextField alloc] init];
    myTextField.backgroundColor=[UIColor yellowColor];
    myTextField.frame=CGRectMake(10, 130, 80, 20);
    myTextField.bk_shouldBeginEditingBlock=^(UITextField *textField) {
        NSLog(@"shouldBeginEditingBlock");
        return YES;
    };
    myTextField.bk_shouldBeginEditingBlock = ^(UITextField *textField) { NSLog(@"shouldBeginEditingBlock");
        return YES;
    };
    myTextField.bk_didBeginEditingBlock = ^(UITextField *textField) { NSLog(@"didBeginEditingBlock");
    };
    myTextField.bk_shouldEndEditingBlock = ^(UITextField *textField) { NSLog(@"shouldEndEditingBlock");
        return YES;
    };
    myTextField.bk_didEndEditingBlock = ^(UITextField *textField) { NSLog(@"didEndEditingBlock ");
    };
    myTextField.bk_shouldChangeCharactersInRangeWithReplacementStringBlock = ^(UITextField *textField, NSRange range, NSString *replacement) { NSLog(@"shouldChangeCharactersInRangeWithReplacementStringBlock"); return YES;
    };
    myTextField.bk_shouldClearBlock = ^(UITextField *textField) { NSLog(@"shouldClearBlock");
        return YES;
    };
    myTextField.bk_shouldReturnBlock = ^(UITextField *textField) { NSLog(@"shouldReturnBlock");
        return YES;
    };
    [self.view addSubview:myTextField];