【发布时间】:2014-05-20 18:48:42
【问题描述】:
我有一个以轮播方式显示信息的滚动视图,我现在要做的是在轮播中的每个视图上添加点击事件,我可以通过添加 UITapGestureRecongnizer* 每次推送时,都会记录一个错误*嵌套推送动画会导致导航栏损坏,并且会显示错误信息 下面是我加载 Carousel View 和添加 Click 事件的代码,Carousel 上的数据也是从 jSON 获取并与 NSObject 一起存储在 **NSMutableArray* ...
-(void)updateUI:(NSMutableArray *)array {
CGFloat contentOffset = 0.0f;
for (NSString *dis in carouselArray) {
CGRect frame = CGRectMake(contentOffset, 0.0f, responseScroll.frame.size.width, responseScroll.frame.size.height);
UIView *views = [[UIView alloc] initWithFrame:frame];
views.backgroundColor = [UIColor clearColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 130)];
imageView.contentMode = UIViewContentModeScaleToFill;
//imageView.image = [UIImage imageNamed:@"banner.png"];
UITapGestureRecognizer *imageMove = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moreViewMove)];
imageMove.cancelsTouchesInView = NO;
NSString *urls = [dis valueForKey:@"imageURL"];
if ([urls isEqual: @""]) {
imageView.image = [UIImage imageNamed:@"banner.png"];
} else {
[imageView setImageWithURL:[NSURL URLWithString:urls]];
}
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(10, 132, 152, 21)];
name.font = [UIFont fontWithName:@"Helvetica Bold" size:13];
name.textColor = [UIColor blackColor];
name.text = [dis valueForKey:@"name"];
UILabel *address = [[UILabel alloc] initWithFrame:CGRectMake(10, 149, 194, 21)];
address.font = [UIFont fontWithName:@"Helvetica Light" size:12];
address.textColor = [UIColor blackColor];
address.text = [dis valueForKey:@"address"];
UILabel *km = [[UILabel alloc] initWithFrame:CGRectMake(278, 133, 42, 21)];
km.font = [UIFont fontWithName:@"Helvetica" size:11];
CLLocation *current = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];
CLLocation *itemLoc = [[CLLocation alloc] initWithLatitude:[[dis valueForKey:@"lat"] doubleValue] longitude:[[dis valueForKey:@"lon"] doubleValue]];
CLLocationDistance itemDist = [itemLoc distanceFromLocation:current]/1000;
//NSLog(@"Distance: %f", itemDist);
km.text = [[NSString alloc] initWithFormat:@"%.2fkm", itemDist];
views.userInteractionEnabled = YES;
[views addGestureRecognizer:imageMove];
_starRating = [[EDStarRating alloc] initWithFrame:CGRectMake(234, 149, 78, 16)];
_starRating.starImage = [UIImage imageNamed:@"star.png"] ;
_starRating.starHighlightedImage = [UIImage imageNamed:@"starhighlighted.png"];
_starRating.maxRating = 5.0;
_starRating.delegate = self;
_starRating.horizontalMargin = 0;
_starRating.editable=NO;
_starRating.rating= [[dis valueForKey:@"rating"] floatValue];
_starRating.displayMode=EDStarRatingDisplayHalf;
[views addSubview:imageView];
[views addSubview:name];
[views addSubview:address];
[views addSubview:km];
[views addSubview:_starRating];
[responseScroll setUserInteractionEnabled:YES];
[responseScroll addSubview:views];
[responseScroll addGestureRecognizer:imageMove];
contentOffset += views.frame.size.width;
responseScroll.contentSize = CGSizeMake(contentOffset, views.frame.size.height);
}
}
处理查看点击事件的代码如下,我知道我做错了但我不知道该怎么做。
-(void)moreViewMove {
for (NSString *dat in carouselArray) {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingPathComponent:@"u_id.plist"];
NSMutableDictionary *dico = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSString *place_id = [dat valueForKey:@"place_id"];
NSString *place_reference = [dat valueForKey:@"place_reference"];
CLLocation *current = [[CLLocation alloc] initWithLatitude:startLocation.coordinate.latitude longitude:startLocation.coordinate.longitude];
CLLocation *itemLoc = [[CLLocation alloc] initWithLatitude:[[dat valueForKey:@"lat"] floatValue] longitude:[[dat valueForKey:@"lon"] floatValue]];
CLLocationDistance itemDist = [itemLoc distanceFromLocation:current]/1000;
UIDevice *device = [UIDevice currentDevice];
NSString *u_id = [[device identifierForVendor] UUIDString];
if ([dico objectForKey:@"u_id"]) {
moreView *more = [self.storyboard instantiateViewControllerWithIdentifier:@"MoreView"];
more.names = [dat valueForKey:@"name"];
more.currentLat = [[NSString alloc] initWithFormat:@"%f", locationManager.location.coordinate.latitude];
more.currentLon = [[NSString alloc] initWithFormat:@"%f", locationManager.location.coordinate.longitude];
more.destinationLon = [dat valueForKey:@"lon"];
more.destinationLat = [dat valueForKey:@"lat"];
more.addressL = [dat valueForKey:@"address"];
more.kilo = [[NSString alloc] initWithFormat:@"%.2fkm", itemDist];
more.dataURL = @{@"type": @"details",@"u_id":[dico objectForKey:@"u_id"], @"place_id": place_id, @"place_reference": place_reference, @"device":@"server"};
[self.navigationController pushViewController:more animated:YES];
}
}
}
请,任何帮助将不胜感激...谢谢
【问题讨论】:
标签: ios objective-c uiviewcontroller uiscrollview