gpt4 book ai didi

ios - uiscrollview 中的多个 ImageView 在旋转设备时产生问题

转载 作者:行者123 更新时间:2023-11-29 01:21:44 24 4
gpt4 key购买 nike

在ViewController的viewdidload中我写了这段代码

 self.navigationController.navigationBarHidden = YES;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
arrImage = [[NSMutableArray alloc]initWithObjects:@"IMG_0621.PNG",@"IMG_0643.PNG",@"IMG_0658.PNG",@"IMG_0701.PNG", nil];
if (!scrollView) {
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320 , 568)];
}else
scrollView.frame = CGRectMake(0, 0, 320 , 568);
[scrollView setAlwaysBounceVertical:NO];
[scrollView setAlwaysBounceHorizontal:NO];
scrollView.backgroundColor = [UIColor cyanColor];
scrollView.pagingEnabled = YES;
for (int i=0; i<arrImage.count; i++) {
CGFloat xOrigin = i * scrollView.frame.size.width;
imageview = [[UIImageView alloc]initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
imageview.backgroundColor = [UIColor redColor];
imageview.userInteractionEnabled = YES;
NSString *strImage = [arrImage objectAtIndex:i];
UIImage *image = [UIImage imageNamed:strImage];
[imageview setImage:image];
[scrollView addSubview:imageview];
}
scrollView.contentOffset = CGPointMake(0, 0);
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * [arrImage count], scrollView.frame.size.height)];
[self.view addSubview:scrollView];

在我的通知中,我写了这个

UIDevice * device = note.object;
switch(device.orientation){
case UIDeviceOrientationPortrait:{
scrollView.frame = CGRectMake(0, 0, 320,568);
scrollView.pagingEnabled = YES;

for (int i =0; i<arrImage.count; i++) {
CGFloat xOrigin = i * scrollView.frame.size.width;
imageview.frame = CGRectMake(xOrigin, 0, 320,568);

}
[scrollView setContentSize:CGSizeMake(320 * [arrImage count], 568)];
}break;

case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationFaceDown:
break;
case UIDeviceOrientationFaceUp:
break;
case UIDeviceOrientationLandscapeLeft:{
scrollView.frame = CGRectMake(0, 0, 568,320);
scrollView.pagingEnabled = YES;
for (int i =0; i<arrImage.count; i++) {
CGFloat xOrigin = i * 568;
imageview.frame = CGRectMake(xOrigin, 0, 568,320);
imageview.backgroundColor = [UIColor redColor];
}
[scrollView setContentSize:CGSizeMake(568 * [arrImage count], 320)];
}
break;
case UIDeviceOrientationLandscapeRight:{
scrollView.frame = CGRectMake(0, 0, 568,320);
scrollView.pagingEnabled = YES;

for (int i =0; i<arrImage.count; i++) {
CGFloat xOrigin = i * 568;
imageview.frame = CGRectMake(xOrigin, 0, 568,320);
}
[scrollView setContentSize:CGSizeMake(568 * [arrImage count], 320)];
}break;
case UIDeviceOrientationUnknown:

break;
default:
break;
};

当方向改变时,我的 ScrollView 改变了它的框架,但我在 ScrollView 中的 ImageView 没有根据方向改变框架。我也尝试过 imageview transform 但对我有用。

最佳答案

根据我的理解,您应该使用 UICollectionView 而不是 UIScrollView,但是正如您提到的,您不能使用 collectionView,那么在这种情况下,您需要检查已应用于 UIImageView 的约束。

下面是我的演示截图。

纵向:

Portrait

风景: Landscape

以下是我已应用但会根据您的需要和满意度进行更改的约束的屏幕截图。

ScrollView 约束 ss1

关于 UIScrollView 的 UIView 约束之一 ss2

注意:在您的情况下,您需要以编程方式应用约束。

关于ios - uiscrollview 中的多个 ImageView 在旋转设备时产生问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34510872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com