iOS移动开发iOS 优秀实践IOS

UI基础

2015-07-24  本文已影响373人  木_木27

类扩展(Class Extension)

@interface 类名()
/* 属性、成员变量、方法声明 */
@end
@interface 类名(分类名字)
/* 方法声明 */
@end
@implementation 类名(分类名字)
/* 方法实现 */
@end
- 分类只能扩充方法
- 如果在分类中声明了一个属性,分类只会生成这个属性的get\set方法声明,`并不会生成成员变量`

项目的常见属性


九宫格的计算方式

/**************  一些常用的变量 begin **************/
// 每一行的列数
NSUInteger colsPerRow = 3;
// 获得当前商品的索引
NSUInteger index = self.shopsView.subviews.count;
// 商品宽度
CGFloat shopW = 70;
// 商品高度
CGFloat shopH = 90;
/**************  一些常用的变量 end **************/
    
/**************  计算X值 begin **************/
// 求出列号
NSUInteger col = index % colsPerRow;
// 每一列之间的间距
CGFloat xMargin = (self.shopsView.frame.size.width - colsPerRow * shopW) / (colsPerRow - 1);
// 商品X
CGFloat shopX = (shopW + xMargin) * col;
/**************  计算X值 end **************/
    
    
/**************  计算Y值 begin **************/
// 求出行号
NSUInteger row = index / colsPerRow;
// 每一行之间的间距
CGFloat yMargin = 20;
// 商品Y
CGFloat shopY = (shopH + yMargin) * row;
/**************  计算X值 end **************/

UIImageView

// 内容模式:一般用来控制图片如何显示
imageView.contentMode = UIViewContentModeScaleAspectFit;
// 裁剪超出imageView边框的部分
imageView.clipsToBounds = YES;
// 设置动画图片
self.imageView.animationImages;
// 设置播放次数
self.imageView.animationRepeatCount
// 设置动画的时间
self.imageView.animationDuration
// 开始动画
[self.imageView startAnimating];

contentMode属性

小语法点

imageView.frame.size = imageView.image.size;
CGRect tempFrame = imageView.frame;
tempFrame.size = imageView.image.size;
imageView.frame = tempFrame;

initWithImage:方法

修改frame的3种方式

imageView.frame = CGRectMake(100, 100, 200, 200);
CGRect tempFrame = imageView.frame;
tempFrame.origin.x = 100;
tempFrame.origin.y = 100;
tempFrame.size.width = 200;
tempFrame.size.height = 200;
imageView.frame = tempFrame;
imageView.frame = (CGRect){{100, 100}, {200, 200}};
UIImage *image = [UIImage imageNamed:@"图片名"];
//使用场合:图片比较小、使用频率较高
//建议把需要缓存的图片直接放到Images.xcassets
NSString *file = [[NSBundle mainBundle] pathForResource:@"图片名" ofType:@"图片的扩展名"];
UIImage *image = [UIImage imageWithContentsOfFile:@"图片文件的全路径"];
//使用场合:图片比较大、使用频率较小
//不需要缓存的图片不能放在Images.xcassets
//放在Images.xcassets里面的图片,只能通过图片名去加载图片

UIControl


UILabel

UILabel *la = [[UILabel alloc] init];
la.lineBreakMode = NSLineBreakByWordWrapping;//保持单词的完整性

@property的使用策略


自定义控件的步骤

1、通过纯代码自定义控件

2、通过xib自定义控件


中间的提醒内容

创建颜色

+ (UIColor *)blackColor;      // 0.0 white
+ (UIColor *)darkGrayColor;   // 0.333 white
+ (UIColor *)lightGrayColor;  // 0.667 white
+ (UIColor *)whiteColor;      // 1.0 white
+ (UIColor *)grayColor;       // 0.5 white
+ (UIColor *)redColor;        // 1.0, 0.0, 0.0 RGB
+ (UIColor *)greenColor;      // 0.0, 1.0, 0.0 RGB
+ (UIColor *)blueColor;       // 0.0, 0.0, 1.0 RGB
+ (UIColor *)cyanColor;       // 0.0, 1.0, 1.0 RGB
+ (UIColor *)yellowColor;     // 1.0, 1.0, 0.0 RGB
+ (UIColor *)magentaColor;    // 1.0, 0.0, 1.0 RGB
+ (UIColor *)orangeColor;     // 1.0, 0.5, 0.0 RGB
+ (UIColor *)purpleColor;     // 0.5, 0.0, 0.5 RGB
+ (UIColor *)brownColor;      // 0.6, 0.4, 0.2 RGB
+ (UIColor *)clearColor;      // 透明色

按钮

// 设置按钮内容的内边距(影响到imageView和titleLabel)
@property(nonatomic) UIEdgeInsets contentEdgeInsets;
// 设置titleLabel的内边距(影响到titleLabel)
@property(nonatomic) UIEdgeInsets titleEdgeInsets;
// 设置imageView的内边距(影响到imageView)
@property(nonatomic) UIEdgeInsets imageEdgeInsets;

图片拉伸

// 只拉伸中间的1x1区域
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;

拷贝(copy)

copy

KVC

// 能修改私有成员变量
- (void)setValue:(id)value forKey:(NSString *)key;
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;
// 能取得私有成员变量的值
- (id)valueForKey:(NSString *)key;
- (id)valueForKeyPath:(NSString *)keyPath;
- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;

KVO

// 利用b对象来监听a对象name属性的改变
[a addObserver:b forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"test"];
 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change     context:(void *)context
    {
        NSLog(@"%@ %@ %@ %@", object, keyPath, change, context);
    }

代理的使用步骤

@property (nonatomic, weak) id<XMGWineCellDelegate> delegate;
xxx.delegate = yyy;
if ([self.delegate respondsToSelector:@selector(wineCellDidClickPlusButton:)]) {
    [self.delegate wineCellDidClickPlusButton:self];
}

iOS监听某些事件的方法


数据刷新(UITableView数据)

1.全局刷新(最常用的方法)

[self.tableView reloadData];

2.局部刷新

//在index插入一个模型数据
-(void)insertObject:(id)anObject atIndex:(NSUInteger)index;
//添加模型
- (void)addObject:(id)anObject;
-(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
//移除最后一个模型
- (void)removeLastObject;
//移除index位置的的数据模型
- (void)removeObjectAtIndex:(NSUInteger)index;
//用anObject取代index位置的模型
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
-(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
-(void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

左滑删除功能

//只要实现了这个方法,左滑出现Delete按钮的功能就有了;点击了“左滑出现的Delete按钮”也会调用这个方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
//修改右侧Delete的文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了关注");
        
        // 收回左滑出现的按钮(退出编辑模式)
        tableView.editing = NO;
    }];
    
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [self.wineArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
      return @[action1, action0];
}

批量删除功能

//开启批量删除功能
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
//开启批量删除功能后,可以编辑
self.tableView.allowsMultipleSelectionDuringEditing = YES;
上一篇 下一篇

猜你喜欢

热点阅读