-(void)viewDidLoad {
[super viewDidLoad];
NSURL *blogURL = [NSURL URLWithString:@"http://www.example.com/apps/mainslider.php"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSArray *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
data = [dataDictionary valueForKey:@"slider_image"];
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] init ];//WithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+40)];//0, 264, 480, 36
[pgCtr setTag:12];
pgCtr.numberOfPages=0;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Skip" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor colorWithRed:247/255.0 green:183/255.0 blue:25/255.0 alpha:1]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 490, 160.0, 40.0);
[self.view addSubview:button];
}
-(void)setupScrollView:(UIScrollView*)scrMain {
for (int i=0; i<data.count; i++) {
// create image
image = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:@"http://www.example.com/uploads/appbanners/%@",[data objectAtIndex:i]]]]];
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*[data count], scrMain.frame.size.height)];
// enable timer after each 2 seconds for scrolling.
[NSTimer scheduledTimerWithTimeInterval:8 target:self selector:@selector(scrollingTimer) userInfo:nil repeats:YES];
}
- (void)scrollingTimer {
// access the scroll view with the tag
UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
// same way, access pagecontroll access
UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
// get the current offset ( which page is being displayed )
CGFloat contentOffset = scrMain.contentOffset.x;
// calculate next page to display
int nextPage = (int)(contentOffset/scrMain.frame.size.width) + 1 ;
// if page is not 10, display it
if( nextPage!=imagesCount ) {
[scrMain scrollRectToVisible:CGRectMake(nextPage*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=nextPage;
} else {
[scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=0;
}
}
答案 0 :(得分:0)
正如@Uttah Sinha已经指出你为图像创建CGRect的代码似乎是错误的......
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];
应该是
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];
您也会遇到标记UIImageView的问题,当i
为1时,scrollView和ImageView都具有相同的tag
数字,如果你有超过12张图片。您可能需要考虑向ImageView添加偏移量,例如
imgV.tag=i+100;