app开发iOS开发历程实用技术

10.项目实战 百思不得姐 设置图片,视频,声音图片控件的内容

2016-02-01  本文已影响1053人  Liwx

@(iOS 项目实战)[项目实战]


目录


1.计算帖子cell的高度(暂不考虑中间图片控件)

因为中间图片控件显示的图片的尺寸(宽高)是由服务器返回的数据提供,所以此次不能使用自动计算高度的方式,需手动计算cell高度.


计算帖子cell高度的方式一(使用字典缓存方式)


计算帖子cell高度的方式一(通过模型数据计算cell高度)

使用模型数据计算cell高度,为模型额外添加cellHeight属性,用户存放每个模型对应cell的高度,更利于程序封装性.

          // WXAllViewController.m控制器的viewDidLoad方法中设置tableView背景色,并取消原来的分割线.
          self.tableView.backgroundColor = WXColor(206, 206, 206);
          self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
          
          // WXTopicItem.m文件中重写,设置每个cell之间的间隔为10
          - (void)setFrame:(CGRect)frame
          {
              frame.size.height -= WXMargin;
              [super setFrame:frame];
          }

tableView估算高度的简单使用


2.计算中间图片控件的frame


计算中间图片控件的frame

// ------------------------------------------------------------------------
// 3.中间图片控件高度,等比例填充方式显示图片
// 3.1 判断如果不是文字段子,累加图片控件高度
if (self.type != WXTopicTypeWord) {
    // 3.2 计算图片控件实际的frame
    // centerW / centerH = self.width / self.height;
    CGFloat centerW = textMaxW;
    CGFloat centerH = centerW * self.height / self.width;
    CGFloat centerX = WXMargin;
    CGFloat centerY = _cellHeight;
    self.centerFrame = CGRectMake(centerX, centerY, centerW, centerH);
    
    _cellHeight += centerH + WXMargin;
}

3.使用xib自定义中间图片控件(图片,声音,视频类型的图片控件)

据分析,三种类型的图片控件都是用xib方式创建,无需多处都用[[NSBundle mainBundle] loadNibNamed:方法从xib创建图片控件,可以考虑封装UIView+Init的分类.UIView+Init分类中封装加载同名xib的类方法.这样就无需在每个图片控件类中实现快速从xib创建控件的类方法.参考代码如下.

// ----------------------------------------------------------------------------
// 从xib创建view
+ (instancetype)wx_viewFromXib
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
}

使用xib自定义三种图片控件

// WXTopicCell.m中的setTopicItem:方法中添加控制中间图片控件的隐藏/显示
// ------------------------------------------------------------------------
// 设置中间图片控件的隐藏/显示
if (self.topicItem.type == WXTopicTypePicture) {        // 图片
    self.pictureView.hidden = NO;
    self.voiceView.hidden = YES;
    self.videoView.hidden = YES;
} else if (self.topicItem.type == WXTopicTypeVoice) {   // 声音
    self.pictureView.hidden = YES;
    self.voiceView.hidden = NO;
    self.videoView.hidden = YES;
} else if (self.topicItem.type == WXTopicTypeVideo) {   // 视频
    self.pictureView.hidden = YES;
    self.voiceView.hidden = YES;
    self.videoView.hidden = NO;
} else if (self.topicItem.type == WXTopicTypeWord) {    // 文字
    self.pictureView.hidden = YES;
    self.voiceView.hidden = YES;
    self.videoView.hidden = YES;
}

// ----------------------------------------------------------------------------
// 布局子控件
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // ------------------------------------------------------------------------
    // 布局中间图片控件
    if (self.topicItem.type == WXTopicTypePicture) {        // 图片
        self.pictureView.frame = self.topicItem.centerFrame;
    } else if (self.topicItem.type == WXTopicTypeVoice) {   // 声音
        self.voiceView.frame = self.topicItem.centerFrame;
    } else if (self.topicItem.type == WXTopicTypeVideo) {   // 视频
        self.videoView.frame = self.topicItem.centerFrame;
    }
}



tableView补充


4.设置声音,视频图片控件的内容

设置声音和视频图片控件的功能实现方式基本一致.所以只对声音图片控件的实现进行描述,不再对视频图片控件的实现进行累述.



设置声音图片控件的内容


封装分类UIImageView+Image,实现通过判断网络状态下载对应的图片


声音视频类型帖子运行效果.gif

5.设置图片控件的内容(长图,动态图,普通图)

通过url判断是静态图还是动态图

    // gif的url,注意大小写.
    http://ww4.sinaimg.cn/large/61129224jw1f139h1eop6g207804ax6q.GIF
    http://ww4.sinaimg.cn/large/61129224jw1f139h1eop6g207804ax6q.jpg

设置图片控件内容

// 超过屏幕高度的图片为长图
if (centerH >= XMGScreenH) {
    centerH = 200;
    self.bigPicture = YES;
}

声音,视频,图片类型帖子运行效果.gif

存在问题


补充

xib命名时需注意

SDWebImage补充

上一篇 下一篇

猜你喜欢

热点阅读