此类新建完成,在自定义控件中的应用如下:(此自定义控件是一个上传图片的scrollVIew)
新建自定义控件类编辑UIBaseScrollView.h如下
//
// UIBaseScrollView.h
// 作业整理
//
// Created by apple on 15/9/16.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "UIBaseVIew.h"
#import "ImageAndPhotos.h"
@interface UIBaseScrollView : UIBaseVIew<PhotoButtonDelegate>
@property (nonatomic, strong) NSMutableArray *arrayImgs;
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) ImageAndPhotos *imgChange;
@property (nonatomic, strong) UIButton *btnImg;
@property (nonatomic, strong) UIImageView *imgV;
-(id)initWithFrame:(CGRect)frame CurrenContr:(UIViewController *) crtl;
@end
编辑定义控件的.m文件如下:
[objc] view plain copy
//
// UIBaseScrollView.m
// 作业整理
//
// Created by apple on 15/9/16.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "UIBaseScrollView.h"
@implementation UIBaseScrollView
-(id)initWithFrame:(CGRect)frame CurrenContr:(UIViewController *) crtl
{
if (self = [super initWithFrame:frame]) {
self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.btnImg = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, frame.size.height-20, frame.size.height-20)];
[self.btnImg setImage:[UIImage imageNamed:@"tizhong_photo_increase_bj"] forState:UIControlStateNormal];
self.imgChange = [[ImageAndPhotos alloc] initWithControler:crtl AndButton:self.btnImg];
self.scroll.showsHorizontalScrollIndicator = YES;
self.imgChange.delegate = self;
[self.scroll addSubview:self.btnImg];
[self addSubview:self.scroll];
}
return self;
}
-(void)setPhotoButton:(ImageAndPhotos *)imgAndP
{
NSLog(@"%@&&&&&&&&&",self.imgChange.img);
if (imgAndP.img) {
self.imgV =[[UIImageView alloc] initWithFrame: self.btnImg.frame ];
self.imgV.image = imgAndP.img;
self.imgV.backgroundColor = [UIColor yellowColor];
[self.scroll addSubview:self.imgV];
self.btnImg.frame = CGRectMake(CGRectGetMaxX(self.imgV.frame)+10, self.imgV.frame.origin.y, self.imgV.frame.size.width, self.imgV.frame.size.height);
self.scroll.contentSize = CGSizeMake(CGRectGetMaxX(imgAndP.btn.frame)+10, 0);
if (CGRectGetMaxX(self.btnImg.frame)>self.scroll.frame.size.width) {
self.scroll.contentOffset = CGPointMake(self.btnImg.frame.origin.x-10, 0);
}
}
}
@end
在控制器中使用此自定义控件如下:
UIBaseScrollView *det5 = [[UIBaseScrollView alloc] initWithFrame:CGRectMake
(20, CGRectGetMaxY(det4.frame)+20, WIDTH-40, 80) CurrenContr:self];
运行结果如下:










