UICollectionView点击效果

 

本人初学ios,顺便写下笔记吧。 本文地址:http://blog.csdn.net/lanqi_x/article/details/53023827

UICollectionView的item点击效果需要自己实现,系统提供了

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;

两个方法,对应点击时和松开时的事件处理。(顺便说句,个人觉得oc的方法命名好烦)

-(void) collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
//设置选中时的颜色
    LQManageCell *cell = (LQManageCell*)[collectionView cellForItemAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor string2Color:@"#e5e5e5"]];
}

- (void)collectionView:(UICollectionView *)colView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
//恢复颜色
    LQManageCell *cell = (LQManageCell*)[colView cellForItemAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor clearColor]];
    
}
实现这两个方法后会发现单击并没有看到效果,只有长按才能看到颜色变化。要实现单击就能看到点击效果需设置UICollectionView的delaysContentTouches属性为false。
self.collectionView.delaysContentTouches = false;

随便记录下16进制转成UIColor的方法,使用类别来扩展UIColor的方法,java没有这种方式,个人觉得貌似还挺好用的。

#import "UIColor+LQColor.h"

@implementation UIColor (LQColor)

+ (UIColor *) string2Color:(NSString *)str
{
    if (!str || [str isEqualToString:@""]) {
        return nil;
    }
    unsigned red,green,blue;
    NSRange range;
    range.length = 2;
    range.location = 1;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
    range.location = 3;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
    range.location = 5;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
    UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
    return color;
}

@end
   
 
 
 
发布了13 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/lanqi_x/article/details/53023827