1.UITapGestureRecognizer传值
在对UIView等添加手势时,若想通过UITapGestureRecognizer传值,那么可以传tag
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(UesrClicked:)];
singleTap.view.tag = i;
[imageView addGestureRecognizer:singleTap];
然后根据tag做不同操作
2.同一个View里边添加两个collectionView时,即使这两个collectionView的layout是一样的,也不能公用同一个layout,否则会出bug,不一定崩溃,但也会出现其他问题。
3.遍历比较两个数组,可以用谓词NSPredicate。下面用例子说明
筛选arrayContents包含了哪些arrayFilter元素
self.arrayFilter = [NSArray arrayWithObjects:@"pict", @"blackrain", @"ip", nil];
self.arrayContents = [NSMutableArray arrayWithObjects:@"I am a picture.", @"I am a guy", @"I am gagaga", @"pict", @"iphone", nil];
有三种方法来实现,最原始的使用两个for循环,最简洁的是用NSPredicate
//原始方法,两个for
-(void)emunateArrTwoFor{
for (NSString *content in self.arrayContents) {
for (NSString *filter in self.arrayFilter) {
if ([filter isEqualToString:content]) {
NSLog(@"相同元素为%@",content);
}
}
}
}
//改进方法,一个for
-(void)emunateArrOneFor{
NSArray *temp = [NSArray array];
for (NSUInteger i=0; i<self.arrayFilter.count; i++) {
NSString *filter = [self.arrayFilter objectAtIndex:i];
NSPredicate *predict = [NSPredicate predicateWithFormat:@"self = %@",filter];
temp = [self.arrayContents filteredArrayUsingPredicate:predict];
if (temp.count > 0) {
NSLog(@"-----%@",temp);
}
}
}
//最简洁方法,没有for
-(void)emunateArrNoFor{
NSPredicate *predict = [NSPredicate predicateWithFormat:@"self in %@",self.arrayFilter];
NSArray *temp = [self.arrayContents filteredArrayUsingPredicate:predict];
NSLog(@"+++++%@",temp);
}
打印结果为
NSPredicate的关键词有in,contains,=,>,<等
4.UICollectionViewCell上有btn,btn如何获取cell的indexPath。可以这样来,这是苹果官方的做法
[cell.btn addTarget:self action:@selector(clickCatoryBtn:) forControlEvents:UIControlEventTouchUpInside];
-(void)clickCatoryBtn:(id)sender{
CGPoint btnPoint = [sender convertPoint:CGPointZero toView:_collectionView];
NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:btnPoint];
if (nil != indexPath) {
//按钮点击处理
}
}
5.等比例缩放图片及问题
这是通过一个不大于1的缩放系数来实现,写在UIImage的category中。
+(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize;
先看普通的
+(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
但这样会出现失真问题,图片会变得模糊。应用下边的
+(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{
CGSize newSize = CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
6.app版本号比较
由于一般的版本号格式都是类似于“3.1.1”这样的,所以不能用floatValue比较。这时可以用
NSString *num1 = @"5.2.0";
NSString *num2 = @"5.3.0";
if ([num1 compare:num2 options:NSNumericSearch] == NSOrderedDescending) {
NSLog(@"%@ is bigger",num1);
} else {
NSLog(@"%@ is bigger",num2);
}
其中NSOrderedDescending是降序排列
###更新---补充版本号相等的情况###
上述写法有缺陷,当版本号相同时也会提示,应该添加相同情形时才完整
if ([num1 compare:num2 options:NSNumericSearch] == NSOrderedDescending) {
NSLog(@"%@ is bigger",num1);
} else if([num1 isEqualToString:num2]){
NSLog(@"same");
} else {
NSLog(@"%@ is bigger",num2);
}