Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DLMultiSectionControl
//
// DLMultiSelectionControl.h
// DLMultiSelectionControl
//
// Created by Darcy Liu on 8/31/14.
// Copyright (c) 2014 Close To U. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DLMultiSelectionControl : UIControl
@property (nonatomic,strong) NSArray *items;
@property (nonatomic,strong) NSArray *titles;
- (NSArray *)indexPathsForSelectedItems;
- (void)selectAllItems;
- (void)reset;
@end
//
// DLMultiSelectionControl.m
// DLMultiSelectionControl
//
// Created by Darcy Liu on 8/31/14.
// Copyright (c) 2014 Close To U. All rights reserved.
//
#import "DLMultiSelectionControl.h"
@interface UIImage (DLHighlightdImage)
- (UIImage*)_dlHighlightdImage;
@end
@implementation UIImage (DLHighlightdImage)
// this mehtod source form WBHighlightImage
// http://www.platinumball.net/blog/2011/01/31/uibutton-graphics-highlighting/
- (UIImage*)_dlHighlightdImage
{
UIImage *image = self;
const CGSize size = image.size;
const CGRect bnds = CGRectMake(0.0, 0.0, size.width, size.height);
UIColor* colr = nil;
UIImage* copy = nil;
CGContextRef ctxt = NULL;
// this is the mask color
colr = [[UIColor alloc] initWithWhite:0 alpha:0.6];
// begin image context
if (UIGraphicsBeginImageContextWithOptions == NULL) {
//UIGraphicsBeginImageContext(bnds.size);
} else {
UIGraphicsBeginImageContextWithOptions(bnds.size, FALSE, 0.0);
}
ctxt = UIGraphicsGetCurrentContext();
// transform CG* coords to UI* coords
CGContextTranslateCTM(ctxt, 0.0, bnds.size.height);
CGContextScaleCTM(ctxt, 1.0, -1.0);
// draw original image
CGContextDrawImage(ctxt, bnds, image.CGImage);
// draw highlight overlay
CGContextClipToMask(ctxt, bnds, image.CGImage);
CGContextSetFillColorWithColor(ctxt, colr.CGColor);
CGContextFillRect(ctxt, bnds);
// finish image context
copy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return copy;
}
@end
@interface DLMultiSelectionItemCell : UICollectionViewCell
@property (nonatomic,strong,readonly) UILabel *textLabel;
@property (nonatomic,strong,readonly) UIImageView *backgroundImageView;
@property (nonatomic,strong,readonly) UIImageView *selectedBackgroundImageView;
@end
@implementation DLMultiSelectionItemCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_backgroundImageView = [UIImageView new];
self.backgroundView = _backgroundImageView;
self.backgroundView.backgroundColor = [UIColor colorWithRed:0.729 green:0.718 blue:0.686 alpha:1.0];
_selectedBackgroundImageView = [UIImageView new];
self.selectedBackgroundView = _selectedBackgroundImageView;
self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.549 green:0.537 blue:0.518 alpha:1.0];
_textLabel = [[UILabel alloc] init];
_textLabel.textAlignment = NSTextAlignmentCenter;
_textLabel.text = @"";
_textLabel.backgroundColor = [UIColor clearColor];
_textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:_textLabel];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
_backgroundImageView.frame = self.bounds;
_selectedBackgroundImageView.frame = self.bounds;
_textLabel.frame = self.bounds;
}
- (void)setSelected:(BOOL)selected
{
[super setSelected:selected];
}
- (void)prepareForReuse
{
[super prepareForReuse];
_textLabel.text = @"";
}
@end
@interface DLMultiSelectionControl()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UICollectionView *collectionView;
@end
@implementation DLMultiSelectionControl
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumLineSpacing = 5;
layout.minimumInteritemSpacing = 5;
layout.itemSize = CGSizeMake(frame.size.height-10, frame.size.height-10);
layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
_collectionView.allowsMultipleSelection = YES;
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor colorWithRed:0.827 green:0.808 blue:0.769 alpha:1.0];
[_collectionView registerClass:[DLMultiSelectionItemCell class] forCellWithReuseIdentifier:@"kCellReuseIdentifier"];
[self addSubview:_collectionView];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
_collectionView.frame = self.bounds;
[_collectionView.collectionViewLayout invalidateLayout];
}
#pragma mark - property
- (void)setItems:(NSArray *)items
{
_items = items;
[self.collectionView reloadData];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [_items count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
DLMultiSelectionItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kCellReuseIdentifier" forIndexPath:indexPath];
if ([[_items objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]) {
cell.textLabel.text = [_items objectAtIndex:indexPath.row];
} else if([[_items objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]]){
UIImage *image = [_items objectAtIndex:indexPath.row];
cell.backgroundImageView.backgroundColor = [UIColor clearColor];
cell.backgroundImageView.image = image;
cell.selectedBackgroundImageView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundImageView.image = [image _dlHighlightdImage];
}
if (indexPath.row<[_titles count] && [[_titles objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]) {
cell.textLabel.text = [_titles objectAtIndex:indexPath.row];
}
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
#pragma mark - Methods
- (NSArray *)indexPathsForSelectedItems
{
return [self.collectionView indexPathsForSelectedItems];
}
- (void)selectAllItems;
{
for (NSInteger i=0;i < [_items count]; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}
}
- (void)reset
{
[self.collectionView reloadData];
}
@end
DLMultiSelectionControl *_ms = [[DLMultiSelectionControl alloc] initWithFrame:CGRectMake(50, 100, 185, 50)];
_ms.titles = @[@"A",@"",@"C",@"D"];
_ms.items = @[[UIImage imageNamed:@"btn_01"],[UIImage imageNamed:@"btn_02"]];
//_ms.items = @[@"A",@"B",@"C",@"D"];
[_ms addTarget:self action:@selector(selectionChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_ms];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.