本人初學ios,順便寫下筆記吧。 本文地址:http://blog.csdn.net/lanqi_x/article/details/53023827UICollectionView的item點擊效果需要自己實現,系統提供了
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;
兩個方法,對應點擊時和鬆開時的事件處理。(順便說句,個人覺得oc的方法命名好煩)
<pre name="code" class="objc">-(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