我有图像数组,我需要在Landscape
andportrait
模式上显示。我所做的是我创建了一个图像数组并显示在 scrollView 上一次看到一个 ..但问题是当我移动时或 Landscape to portrait
反之亦然图像被分割...视图带有上一张图像和下一张图像的一半..这不会每次都发生...我不知道为什么图像会分裂而不是完全对半...有时会分成不同的部分尺寸..
#define portrait_width1 600.0
#define portrait_height1 600.0
#define landscape_width1 800.0
#define landscape_height1 445.0
CGFloat kScrollObjHeight_por1 = portrait_height1;
CGFloat kScrollObjWidth_por1 = portrait_width1;
- (void)viewDidLoad{
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES;
// default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
scrollView1.delegate = self;
scrollView1.pagingEnabled = YES;
[scrollView1 setShowsHorizontalScrollIndicator:NO];
[scrollView1 setShowsVerticalScrollIndicator:NO];
kNumImages=content.count;
for (i = 1; i <= content.count; i++)
{
NSArray *photos = [NSArray arrayWithObjects:
[UIImage imageNamed:@"c.jpg"],
[UIImage imageNamed:@"photo2m.jpg"],[UIImage imageNamed:@"photo1m.jpg"],[UIImage imageNamed:@"photo4m.jpg"],[UIImage imageNamed:@"photo2l.jpg"],[UIImage imageNamed:@"photo4l.jpg"],
nil] ;
UIImage *image = [photos objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight_por1;
rect.size.width = kScrollObjWidth_por1;
rect.origin.y=scrollView1.frame.origin.y;
imageView.frame = rect;
imageView.tag = i;
[scrollArray addObject:imageView];
[scrollView1 addSubview:imageView];
[imageNameArray addObject:[[content objectAtIndex:i-1] _image_url]];
}
[self layoutScrollImages];
}
}
-(void)layoutScrollImages
{
@try {
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
CGRect frame;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth_por1);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth_por1), kScrollObjHeight_por1)];
// scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);
NSLog(@"%d",[imageNameArray indexOfObject:Images]);
currentImage=[scrollArray objectAtIndex:[imageNameArray indexOfObject:Images]];
[scrollView1 scrollRectToVisible:currentImage.frame animated:YES];
}
@catch (NSException *exception) {
NSLog(@"ImageFullScreenViewController - layoutScrollImages Exception Name = %@ Exception Reason = %@",[exception name],[exception reason]);
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
@try {
if ([UIDevice currentDevice].orientation== UIInterfaceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight)
{
if (!landscapeImgSCRf) {
CGFloat curXLoc = 0;
CGRect frame;
for (UIView *subview in scrollView1.subviews)
{
if ([subview isKindOfClass:[UIImageView class]])
{
if (!rightside2f) {
kScrollObjWidth_por1 = landscape_width1;
kScrollObjHeight_por1= landscape_height1;
curXLoc = subview.frame.origin.x;
rightside2f=YES;
}
frame.origin = CGPointMake(curXLoc, 0);
[subview setFrame: CGRectMake(curXLoc, subview.frame.origin.y, kScrollObjWidth_por1, kScrollObjHeight_por1)];
curXLoc += (kScrollObjWidth_por1);
scrollView1.frame=CGRectMake(scrollView1.frame.origin.x, scrollView1.frame.origin.y, scrollView1.frame.size.width, scrollView1.frame.size.height);
}
}
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth_por1), kScrollObjHeight_por1)];
landscapeImgSCRf=YES;
}
}else
{
if (landscapeImgSCRf) {
CGFloat curXLoc = 0;
CGRect frame;
for (UIView *subview in scrollView1.subviews)
{
if ([subview isKindOfClass:[UIImageView class]])
{
if (rightside2f) {
kScrollObjWidth_por1 = portrait_width1;
kScrollObjHeight_por1= portrait_height1;
curXLoc = subview.frame.origin.x;
rightside2f=NO;
}
frame.origin = CGPointMake(curXLoc, 0);
[subview setFrame: CGRectMake(curXLoc, subview.frame.origin.y, kScrollObjWidth_por1, kScrollObjHeight_por1)];
curXLoc += (kScrollObjWidth_por1);
scrollView1.frame=CGRectMake(scrollView1.frame.origin.x, scrollView1.frame.origin.y, scrollView1.frame.size.width, scrollView1.frame.size.height);
}
}
landscapeImgSCRf=NO;
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth_por1), kScrollObjHeight_por1)];
}
}
}
@catch (NSException *exception) {
NSLog(@"ImageFullScreenViewController - willRotateToInterfaceOrientation Exception Name = %@ Exception Reason = %@",[exception name],[exception reason]);
}
}