一个通过点击图片进行刷新的过程
1,图片类
#import <Foundation/Foundation.h>
@interface MYYImageInfo : NSObject
@property(nonatomic,strong)UIImage *image;
@property(nonatomic,strong)NSString *title;
@property(nonatomic,strong)NSString *summary;
@end
@interface MYYImageInfo : NSObject
@property(nonatomic,strong)UIImage *image;
@property(nonatomic,strong)NSString *title;
@property(nonatomic,strong)NSString *summary;
@end
@implementation MYYImageInfo
-(void)dealloc{
self.image = nil;
self.title = nil;
self.summary = nil;
}
@end
-(void)dealloc{
self.image = nil;
self.title = nil;
self.summary = nil;
}
@end
2 图片管理类
#import <Foundation/Foundation.h>
#import "MYYImageInfo.h"
@interface MYYImageInfoManager : NSObject
//希望到得imageinfo对象
+(MYYImageInfo *)imageInfoWithDictionary:(NSDictionary *)dic;
@end
#import "MYYImageInfoManager.h"
@implementation MYYImageInfoManager
+(MYYImageInfo *)imageInfoWithDictionary:(NSDictionary *)dic{
MYYImageInfo *info = [[MYYImageInfo alloc] init];
[info setValuesForKeysWithDictionary:dic];
return info;
}
@end
@implementation MYYImageInfoManager
+(MYYImageInfo *)imageInfoWithDictionary:(NSDictionary *)dic{
MYYImageInfo *info = [[MYYImageInfo alloc] init];
[info setValuesForKeysWithDictionary:dic];
return info;
}
@end
3.,自定义图片视图类
#import <UIKit/UIKit.h>
#import "MYYImageInfo.h"
@interface MYYImageInfoView : UIView
//添加两个属性
@property(nonatomic,strong)id target;
@property(nonatomic,assign)SEL action;
//根据基本对象刷新显示内容
-(void)refreshWithImageInfo:(MYYImageInfo *)info;
@end
#import "MYYImageInfo.h"
@interface MYYImageInfoView : UIView
//添加两个属性
@property(nonatomic,strong)id target;
@property(nonatomic,assign)SEL action;
//根据基本对象刷新显示内容
-(void)refreshWithImageInfo:(MYYImageInfo *)info;
@end
#import "MYYImageInfoView.h"
@implementation MYYImageInfoView{
//图片
UIImageView *imageView;
//图片标题
UILabel *titleLabel;
//图片简介
UILabel *summaryLabel;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//添加图片视图
imageView = [[UIImageView alloc] initWithFrame:self.bounds];
//图片填充
imageView.contentMode = UIViewContentModeScaleAspectFit;
//titlelabel
titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 300, 320, 30)];
//简介视图
summaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 335, 320, CGRectGetHeight(frame) - 335)];
//设置标题效果
titleLabel.font = [UIFont boldSystemFontOfSize:30];
titleLabel.textColor =[UIColor whiteColor];
//设置标签为透明色(在ios6.0中加下面这句话,在ios7.0中不需要)
titleLabel.backgroundColor = [UIColor clearColor];
//设置简介效果
summaryLabel.font = [UIFont boldSystemFontOfSize:20];
summaryLabel.textColor = [UIColor whiteColor];
summaryLabel.backgroundColor = [UIColor clearColor];
summaryLabel.numberOfLines = 0;
//本身就是一个视图
[self addSubview:imageView];
[self addSubview:titleLabel];
[self addSubview:summaryLabel];
//修改视图的底色
self.backgroundColor = [UIColor blackColor];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
-(void)refreshWithImageInfo:(MYYImageInfo *)info{
imageView.image = info.image;
titleLabel.text = info.title;
summaryLabel.text = info.summary;
}
//重写点击事件触发的方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if ([self.target respondsToSelector:self.action]) {
[self.target performSelector:self.action withObject:self];
}
}
@end
4.第一视图控制器
#import "MYYFirstViewController.h"
#import "MYYImageInfoView.h"
#import "MYYImageInfoManager.h"
@interface MYYFirstViewController ()
@property(nonatomic,strong)NSMutableArray *imageInfoList;
@end
@implementation MYYFirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSDictionary *dic1 = @{@"title":@"你好1",@"summary":@"你好简介1",@"image":[UIImage imageNamed:@"1"]};
NSDictionary *dic2 = @{@"title":@"你好2",@"summary":@"你好简介2",@"image":[UIImage imageNamed:@"2"]};
NSDictionary *dic3 = @{@"title":@"你好3",@"summary":@"你好简介3",@"image":[UIImage imageNamed:@"3"]};
NSDictionary *dic4 = @{@"title":@"你好4",@"summary":@"你好简介4",@"image":[UIImage imageNamed:@"4"]};
//创建数组对象
self.imageInfoList = [[NSMutableArray alloc] init ];
[self.imageInfoList addObject:[MYYImageInfoManager imageInfoWithDictionary:dic1]];
[self.imageInfoList addObject:[MYYImageInfoManager imageInfoWithDictionary:dic2]];
[self.imageInfoList addObject:[MYYImageInfoManager imageInfoWithDictionary:dic3]];
[self.imageInfoList addObject:[MYYImageInfoManager imageInfoWithDictionary:dic4]];
//将图片视图添加到视图上去
MYYImageInfoView *infoView = [[MYYImageInfoView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:infoView];
[infoView refreshWithImageInfo:self.imageInfoList[0]];
//target 和action的方法实现
infoView.target = self;
infoView.action = @selector(changeImageInfo:);
}
-(void)changeImageInfo:(MYYImageInfoView *)sender{
NSInteger index = arc4random()% 4;
[sender refreshWithImageInfo:self.imageInfoList[index]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
效果图如下
点击刷新
本文介绍了一个基于iOS的自定义图片视图控件的实现过程,该控件可以通过点击进行图片及信息的刷新。涉及图片类、图片管理类、自定义图片视图类和视图控制器的设计与使用。
955

被折叠的 条评论
为什么被折叠?



