第一:APP 控件的用途。
第二:控件的功能。
第三:关键代码展示。
// ImageUp_View.h
// 图片分布展示
// Created by 周双建 on 16/4/5.
// Copyright © 2016年周双建. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "IMGV.h"
@interface ImageUp_View :UIView<UINavigationControllerDelegate,UIImagePickerControllerDelegate,TapDelegate>{
UIButton * Up_Image ;
@public
int AllCount ;
}
@property(nonnull,assign)UIViewController * MyController;
//add ImageView
@property(nonnull,retain)NSMutableArray * ImageCountArray;
@end
// ImageUp_View.m
// 图片分布展示
//
// Created by 周双建 on 16/4/5.
// Copyright © 2016年周双建. All rights reserved.
//
#import "ImageUp_View.h"
#define IMAGE_HIGHT 60.f
#define IMAGE_WIDTH 60.f
#define IMAGE_PANDER 4.0f
@implementation ImageUp_View
// 重写初始化方法,并创建添加按钮
-(instancetype)initWithFrame:(CGRect)frame{
if (self == [superinitWithFrame:frame]) {
self.ImageCountArray = [NSMutableArrayarrayWithCapacity:0];
Up_Image = [UIButtonbuttonWithType:UIButtonTypeCustom];
Up_Image.frame =CGRectMake(0,0,IMAGE_WIDTH,IMAGE_HIGHT);
[Up_ImagesetTitle:@"添加"forState:UIControlStateNormal];
[Up_ImagesetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
[Up_ImageaddTarget:selfaction:@selector(Up_IMG:)forControlEvents:UIControlEventTouchUpInside];
[self addSubview:Up_Image];
}
return self;
}
//进行方法的实现,相机或者相册的提示选择,并进行系统相册图片的选择
-(void)Up_IMG:(UIButton*)Up_Btn{
//创建图片控制器
UIImagePickerController * Picker = [[UIImagePickerControlleralloc]init];
//创建提示控制器
UIAlertController * AlertController = [UIAlertControlleralertControllerWithTitle:@"请选择照片"message:nilpreferredStyle:UIAlertControllerStyleActionSheet];
// 创建提示控制器上的按钮
UIAlertAction * Action = [UIAlertActionactionWithTitle:@"相册"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
// 设置控制源
Picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
// 设置控制器的代理
Picker.delegate = self ;
// 实现获取图片
[_MyControllerpresentViewController:Pickeranimated:YEScompletion:nil];
}];
// 创建提示控制器上的按钮
UIAlertAction * ActionTwo = [UIAlertActionactionWithTitle:@"相机"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
// 判断手机相机是否可用
BOOL Able = [UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if (Able) {
Picker.sourceType =UIImagePickerControllerSourceTypeCamera;
Picker.delegate = self ;
[_MyControllerpresentViewController:Pickeranimated:YEScompletion:nil];
}else{
// 创建提示控制器上的按钮
UIAlertController * AlertController = [UIAlertControlleralertControllerWithTitle:@"您的手机不支持相机"message:nilpreferredStyle:UIAlertControllerStyleActionSheet];
[_MyControllerpresentViewController:AlertControlleranimated:YEScompletion:nil];
return ;
}
}];
// 创建提示控制器上的按钮
UIAlertAction * ActionThree = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {
}];
// 创建提示控制器上添加按钮
[AlertController addAction:Action];
[AlertController addAction:ActionTwo];
[AlertController addAction:ActionThree];
// 提示的展示方法
[_MyControllerpresentViewController:AlertControlleranimated:NOcompletion:^{
[AlertController removeFromParentViewController];
}];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
// 创建图片接收对象
UIImage * Image = nil;
// 判断你所选择的图片是哪个类型的(没有编辑的和编辑的)
if (info[@"UIImagePickerControllerOriginalImage"]) {
// 没有编辑
Image = info[@"UIImagePickerControllerOriginalImage"];
}else{
// 编辑的
Image = info[@"UIImagePickerControllerEditedImage"];
}
// 清楚图片采集控制器
[picker dismissViewControllerAnimated:YEScompletion:^{
// 将获取的图片对象保存,图片数组里面
[_ImageCountArray addObject:Image];
// 判断是否选择图片和要求最多显示的图片相同
if (_ImageCountArray.count ==AllCount) {
IMGV * Image_V = [[IMGValloc]initWithFrame:CGRectMake((_ImageCountArray.count-1) * IMAGE_WIDTH, 0,IMAGE_WIDTH -IMAGE_PANDER,IMAGE_WIDTH -IMAGE_PANDER)];
Image_V.delegate = self;
Image_V.image =_ImageCountArray[_ImageCountArray.count-1];
[self addSubview:Image_V];
self.frame =CGRectMake(CGRectGetMinX(self.frame),CGRectGetMinY(self.frame), (_ImageCountArray.count+1)*IMAGE_WIDTH,IMAGE_HIGHT);
}else{
IMGV * Image_V = [[IMGValloc]initWithFrame:CGRectMake((_ImageCountArray.count-1) * IMAGE_WIDTH, 0,IMAGE_WIDTH -IMAGE_PANDER,IMAGE_WIDTH -IMAGE_PANDER)];
Image_V.delegate = self;
Image_V.image =_ImageCountArray[_ImageCountArray.count-1];
[self addSubview:Image_V];
self.frame =CGRectMake(CGRectGetMinX(self.frame),CGRectGetMinY(self.frame), (_ImageCountArray.count+1)*IMAGE_WIDTH,IMAGE_HIGHT);
Up_Image.frame =CGRectMake(self.bounds.size.width - IMAGE_WIDTH , 0 ,IMAGE_WIDTH,IMAGE_HIGHT);
}
}];
}
// 实现图片的协议方法
-(void)dismissImage:(UIImageView *)ImageView{
UIImage * ImageOne = ImageView.image;
// 从图片数组中移除你要删除的图片
[_ImageCountArray removeObject:ImageOne];
// 进行重新布局
for (id TempView inself.subviews) {
if ([TempView isMemberOfClass:[IMGV class]]) {
[TempView removeFromSuperview];
}
}
for (int i =0 ;i<_ImageCountArray.count; i++) {
IMGV * Image_V = [[IMGValloc]initWithFrame:CGRectMake(i *IMAGE_WIDTH, 0,IMAGE_WIDTH-IMAGE_PANDER,IMAGE_HIGHT-IMAGE_PANDER)];
Image_V.delegate = self;
Image_V.image = _ImageCountArray[i];
Image_V.userInteractionEnabled =YES;
[self addSubview:Image_V];
}
self.frame =CGRectMake(CGRectGetMinX(self.frame),CGRectGetMinY(self.frame), (_ImageCountArray.count+1)*IMAGE_WIDTH,IMAGE_HIGHT);
Up_Image.frame =CGRectMake(self.bounds.size.width - IMAGE_WIDTH , 0 ,IMAGE_WIDTH,IMAGE_HIGHT);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
// 进行销毁的内存释放
-(void)dealloc{
[Up_ImageremoveFromSuperview];
[_ImageCountArrayremoveAllObjects];
AllCount = 0;
}
@end
// IMGV.h
// 图片分布展示
//
// Created by 周双建 on 16/4/5.
// Copyright © 2016年周双建. All rights reserved.
//
#import <UIKit/UIKit.h>
// 创建一个协议
@protocol TapDelegate <NSObject>
// 协议方法
-(void)dismissImage:(UIImageView*)ImageView;
@end
@interface IMGV : UIImageView
// 协议的实现
@property(nonatomic,weak)id <TapDelegate> delegate;
@end
// IMGV.m
// 图片分布展示
//
// Created by 周双建 on 16/4/5.
// Copyright © 2016年周双建. All rights reserved.
//
#import "IMGV.h"
@implementation IMGV
// 重新初始化,并添加长按手势
-(instancetype)initWithFrame:(CGRect)frame{
if ([superinitWithFrame: frame]) {
self.userInteractionEnabled =YES;
UILongPressGestureRecognizer * Tap = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(TpClick)];
[selfaddGestureRecognizer:Tap];
}
return self;
}
// 删除按钮的添加
-(void)TpClick{
UIButton * DisMissBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
DisMissBtn.frame = CGRectMake(self.bounds.size.width-30,0,30,30);
DisMissBtn.backgroundColor = [UIColorgreenColor];
[DisMissBtn setTitle:@"clean"forState:UIControlStateNormal];
[DisMissBtn addTarget:selfaction:@selector(CleanClick)forControlEvents:UIControlEventTouchUpInside];
[DisMissBtn setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
[self addSubview:DisMissBtn];
}
// 协议方法的实现
-(void)CleanClick{
if (self.delegate && [self.delegaterespondsToSelector:@selector(dismissImage:)]) {
[self.delegatedismissImage:self];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
//
// ViewController.m
// 图片分布展示
//
// Created by 周双建 on 16/4/5.
// Copyright © 2016年周双建. All rights reserved.
//
#import "ViewController.h"
#import "ImageUp_View.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [[UIColorwhiteColor]colorWithAlphaComponent:1.0f];
[self makeNAv];
// 控件的使用
ImageUp_View * IMG = [[ ImageUp_View alloc]initWithFrame:CGRectMake(10,100,60,60)];
// 传递主控制器
IMG.MyController = self;
// 设置显示的数量
IMG -> AllCount = 4;
[self.viewaddSubview:IMG];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)makeNAv{
UIView * Nav_View = [[UIViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,64)];
UIView * Nav_Line = [[UIViewalloc]initWithFrame:CGRectMake(0,CGRectGetHeight(Nav_View.frame)-0.5,CGRectGetWidth(Nav_View.frame),0.5)];
Nav_Line.backgroundColor = [[UIColorblackColor]colorWithAlphaComponent:0.4];
[Nav_View addSubview:Nav_Line];
UILabel * NavLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,20,CGRectGetWidth(Nav_View.frame),CGRectGetHeight(Nav_View.frame) -20)];
NavLabel.text = @"成功QQ吧-提供";
NavLabel.textColor = [[UIColorblueColor]colorWithAlphaComponent:0.75f];
NavLabel.font = [UIFontboldSystemFontOfSize:20];
NavLabel.textAlignment =NSTextAlignmentCenter;
[Nav_View addSubview:NavLabel];
[self.viewaddSubview:Nav_View];
}
@end