您的位置:首页 > 其它

iPhone第六节:代理模式

2015-09-07 20:35 337 查看
代理模式

//代理类,自定义的
#import <UIKit/UIKit.h>

//前向声明
@protocol CustomImageViewDelegate;

@interface CustomImageView : UIView

@property (nonatomic, strong) UIImageView * tapImageView;
@property (nonatomic, strong) UILabel * tapLabel;
@property (weak) id <CustomImageViewDelegate> delegate;

@end

//代理的协议
@protocol CustomImageViewDelegate <NSObject>

//方法,遵循协议的类(实体类)实现该方法
- (void)customImageViewTapOnce:(CustomImageView *)customImageView;

@end

#import "CustomImageView.h"

@implementation CustomImageView

//初始化方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
//位置是相对于自定义的视图的位置
_tapImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
[self addSubview:_tapImageView];

//手势,及响应方法
UITapGestureRecognizer * tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOne:)];
//设置可交互
_tapImageView.userInteractionEnabled = YES;
[_tapImageView addGestureRecognizer:tapOnce];

_tapLabel = [[UILabel alloc] initWithFrame:CGRectMake(_tapImageView.frame.origin.x, _tapImageView.frame.size.height + _tapImageView.frame.origin.y, _tapImageView.frame.size.width, 20)];
_tapLabel.textAlignment = NSTextAlignmentCenter;
_tapLabel.font = [UIFont boldSystemFontOfSize:12.0];
_tapLabel.textColor = [UIColor blackColor];
_tapLabel.backgroundColor = [UIColor clearColor];
[self addSubview:_tapLabel];
}
return self;
}

- (void)tapOne:(UITapGestureRecognizer *)gesture
{
//判断协议的方法是否实现
if ([_delegate respondsToSelector:@selector(customImageViewTapOnce:)])
{
//让实体类去做某些操作
[_delegate customImageViewTapOnce:self];
}
}

@end

#import <UIKit/UIKit.h>
#import "CustomImageView.h"

@interface PhotoViewController : UIViewController <CustomImageViewDelegate>

@end

#import "PhotoViewController.h"
#import "ScrollViewController.h"

@interface PhotoViewController ()

@end

@implementation PhotoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

//摆放图片
for (int i = 0; i < 9; i++)
{
//j = 第i个图片 - 行数 * 4
int j = i - 4 * (i / 4);

//坐标,大小
CustomImageView * customImageView = [[CustomImageView alloc] initWithFrame:CGRectMake(8 + j * 78, 20 + (i / 4) * 98, 70, 90)];
customImageView.tapImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"contentImage%d.jpg", i + 1]];
customImageView.tapLabel.text = [NSString stringWithFormat:@"Image%d", i+ 1];
//代理
customImageView.delegate = self;
[self.view addSubview:customImageView];
}
}

- (void)customImageViewTapOnce:(CustomImageView *)customImageView
{
ScrollViewController * scrollVC = [[ScrollViewController alloc] init];
//传值
scrollVC.detailPhoto = customImageView;
scrollVC.modalTransitionStyle = 1;
[self presentViewController:scrollVC animated:YES completion:nil];
}

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

@end

#import <UIKit/UIKit.h>
#import "CustomImageView.h"

@interface ScrollViewController : UIViewController

//组合,接受值
@property (nonatomic, strong) CustomImageView * detailPhoto;

@end

#import "ScrollViewController.h"

#define SCWIDTH [[UIScreen mainScreen] bounds].size.width
#define SCHEIGHT [[UIScreen mainScreen] bounds].size.height

@interface ScrollViewController () <UIScrollViewDelegate>

{
NSMutableArray * imageArray; //图像数组
UILabel * label; //页数
}

@end

@implementation ScrollViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];

//新建,初始化
imageArray = [[NSMutableArray alloc] initWithCapacity:1];

//滚动视图
UIScrollView * photoScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
photoScrollView.pagingEnabled = YES;
photoScrollView.bounces = NO;

for (int i = 0; i < 9; i++)
{
//添加图片视图和图片
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCWIDTH, (SCHEIGHT - SCWIDTH) / 2, SCWIDTH, SCWIDTH)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"contentImage%d.jpg", i + 1]];

//加入数组
[imageArray addObject:imageView.image];
[photoScrollView addSubview:imageView];
}
//获得点击的图片在数组中的下标
int index = [imageArray indexOfObject:_detailPhoto.tapImageView.image];
//设置内容偏移
photoScrollView.contentOffset = CGPointMake(SCWIDTH * index , 0);
//设置扩展尺寸
photoScrollView.contentSize = CGSizeMake(SCWIDTH * 9, SCHEIGHT);
photoScrollView.delegate = self;
[self.view addSubview:photoScrollView];

//当前页数
label = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, 120, 20)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.text = [NSString stringWithFormat:@"当前%d/9页", index + 1];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:15.0];
[self.view addSubview:label];

//添加返回的手势
UITapGestureRecognizer * tapThree = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)];
tapThree.numberOfTapsRequired = 3;
[self.view addGestureRecognizer:tapThree];
}

//显示当前页数
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int page = scrollView.contentOffset.x / SCWIDTH;
label.text = [NSString stringWithFormat:@"当前%d/9页", page + 1];
}

//返回
- (void)back
{
[self dismissViewControllerAnimated:YES completion:nil];
}

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

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iPhone课程