06-实践
2015.7.15
上午
- 1,自定义UIView
- 2,xib:图形化自定义view
- 什么是xib:xib也是布局界面的描述工具
- 相比storyboard,xib是轻量级的
- 指定xib的custom class,这很重要
- xib的三个重点
- custom class
- freeform 方便操作
- 不用alloc init 而是用loadNibfrom….
- 不用initFrame 用awakeFromNib
~~~objc
+ (instancetype)ShowView
{
return [[[NSBundle mainBundle] loadNibNamed:@”ShopXib” owner:nil options:nil] firstObject];
}
(void)awakeFromNib
{
self.lab.textAlignment = NSTextAlignmentCenter;
}(void)setShop:(UIImage )img lab:(NSString )text
{
[self.imgView setImage:img];
[self.lab setText:text];
}(void)layoutSublayersOfLayer:(CALayer *)layer
{self.imgView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
self.lab.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, 30);
}
~~~
- 3,自定义UIButton布局
- 此生不会忘,layoutSubviews中必须调用[super layoutSubviews]
- 如果没[super layoutSubviews]会到时button的子控件显示不出来
- 当时用了一个钟我都没找出来原因。。所以我记下它了
- 并且由此,可以联想,重写继承自父类的方法的时候,时时刻刻都要想,要不要先调用父类的方法(有些方法是不用调用父类的)
- UIButton有两种布局子控件的实现方法,如下:
~~~objc
(instancetype)initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame:frame])
{
self.titleLabel.textAlignment = NSTextAlignmentCenter;
}return self;
}(CGRect)imageRectForContentRect:(CGRect)contentRect
{
return CGRectMake(0, 0, 70, 70);
}(CGRect)titleRectForContentRect:(CGRect)contentRect
{
return CGRectMake(0, 70, 70, 30);
}
/* 推荐的方法
- (void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake(0, 0, 70, 70);
self.titleLabel.frame = CGRectMake(0, 70, 70, 30);
}
*/
~~~
- 4,渐变动画(两种方式)
- 1,头尾式动画
~~~objc
/* 头尾式动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:3];
self.lb.alpha = 1;
[UIView commitAnimations];
*/
/*
[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDuration:3];
self.lb.alpha = 1;
[UIView commitAnimations];
*/
//#ifdef DEBUG
//UIImageView *imgView = [[UIImageView alloc] init];
/**
* 为什么Animation要放在UIView类方法里?
* 1,只有View才有动画(动画是View的子类,这是肯定的),所以肯定是UIView类
* 2,如果Animation 是单独设计的一个类 这样就有可能有很多动画
而,我自己是知道的,动画是肯定不能分散管理的,因为那样严重消耗资源
,并且难以管理。动画都是用一个总控制方法来控制所以对象的x,y 或者图片改变的
* 3,因为是类方法,所以对象无法调用。注意,这跟java很不同,java是
* 对象也可以调用类方法的!!下面我用了person来做试验证明了。
* 实验证明,oc的类方法只能用类名来调用
oc的对象方法只能用对象来调用
* 苹果这样设计,有什么好处?
*/
// [imgView beg....];
Person *p = [[Person alloc] init];
//[p eat];
//[Person say];
//#endif
(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:3];
self.lb.alpha = 0;
[UIView commitAnimations];
}
~~~
+ block式动画
~~~objc
// block 式动画
/*
[UIView animateWithDuration:3 animations:^{
self.lb.alpha = 1;
}];
[UIView animateWithDuration:3 animations:^{
self.lb.alpha = 1;
} completion:^(BOOL finished) {
[UIView animateWithDuration:3 animations:^{
self.lb.alpha = 0;
}];
}];
*/
~~~
- 5,UIAlertView UIActionSheet UIAlertController
~~~objc
/**
UIAlertView UIActionSheet UIAlertController
:returns: <#return value description#>
*/
/*
UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"标题" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"不要",nil];
[as showInView:self.view];
*/
/*
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"标题" message:@"我得第一个alert" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
*/
/*
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"标题" message:@"我是alertcontroller" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *myAction = [UIAlertAction actionWithTitle:@"zong" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"zong...");
}];
[ac addAction:myAction];
[self presentViewController:ac animated:YES completion:nil];
*/
~~~
6,第三方框架实现提示框
- 核心就是知道怎么使用第三方框架
- 步骤:
- 1,下载第三方框架
- 2,运行Demo,阅读它的文档
- 3,拖第三方框架的相关代码文件到项目中
- 4,调用第三方框架
7,图片拉伸
+必须知道有这么一回事- 图片有Tile(平铺) strech(拉伸)两种模式
~~~objc
/**
* 图片拉伸的三种方法
*/
UIImage *img = [UIImage imageNamed:@”common_button_big_blue_highlighted”];
// The way one
//UIImage *newImg = [img stretchableImageWithLeftCapWidth:img.size.width/2 topCapHeight:img.size.height/2];
// The way two
//UIImage *newImg = [img resizableImageWithCapInsets:UIEdgeInsetsMake(img.size.height/2, img.size.width/2, img.size.height/2, img.size.width/2)];
// The way three
UIImage *newImg = [img resizableImageWithCapInsets:UIEdgeInsetsMake(img.size.height/2, img.size.width/2, img.size.height/2, img.size.width/2) resizingMode:UIImageResizingModeStretch];
[self.bt2 setBackgroundImage:newImg forState:UIControlStateNormal];
~~~
- 8,Button 内边距
- 这看上去事小,但却非常实用,必须牢记
~objc
/**
* Button的内边距
*
* @param 50 <#50 description#>
* @param 0 <#0 description#>
* @param 0 <#0 description#>
* @param 0 <#0 description#>
*
* @return <#return value description#>
*/
//self.bt.titleEdgeInsets = UIEdgeInsetsMake(50, 0, 0, 0);
//self.bt.imageEdgeInsets = UIEdgeInsetsMake(50, 0, 0, 0);
//self.bt.contentEdgeInsets = UIEdgeInsetsMake(50, 0, 0, 0);
~
- 9,KVC
- 10,KVO
- 监听对象的 属性值变化
- 一般对set方法做修改就能实现监听
- 记得一定要再对象销毁前,移除监听
~~~objc
/*
Person *p = [[Person alloc] init];
[p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:@"zong"];
p.name = @"zong";
[p removeObserver:self forKeyPath:@"name"];
*/
- (void)observeValueForKeyPath:(NSString )keyPath ofObject:(id)object change:(NSDictionary )change context:(void *)context
{
NSLog(@”%@,%@,%@,%@”,keyPath,object,change,context);
}
~~~
- 11,做笔记软件:印象笔记?dash