我有一个UIView,其中有一些图像会被隐藏,以便我收到的功能。我一直在寻找,我还没有找到重新定位这些UIImageView的方法,以便将它们全部集中在一起。
我的意思是如果我有以下一行:
|X||X||X||X||X||X||X|
我隐藏了我要展示的第二个和第五个元素
|X||X||X||X||X|
但我得到
|X| |X||X| |X||X|
是否有任何UIView属性来控制它,或者我必须手动创建不同的UIViews或在我的代码上以编程方式添加元素?现在我只使用隐藏了UIImageView元素的属性。
提前致谢!
答案 0 :(得分:2)
尝试这个尺寸! ; - )
的 ViewController.h 强> 的
#define BUFFER 10
#define WIDTH 90
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) NSMutableArray *ViewArray;
- (IBAction)hideViews:(id)sender;
- (void)centerVisibleViews;
@end
的 ViewController.m 强> 的
#import "ViewController.h"
@implementation ViewController
@synthesize ViewArray;
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *colorArray = [NSMutableArray array];
[colorArray addObject:[UIColor redColor]];
[colorArray addObject:[UIColor orangeColor]];
[colorArray addObject:[UIColor yellowColor]];
[colorArray addObject:[UIColor greenColor]];
[colorArray addObject:[UIColor blueColor]];
[colorArray addObject:[UIColor purpleColor]];
[colorArray addObject:[UIColor blackColor]];
[self setViewArray:[NSMutableArray array]];
for (int i = 0; i < 7; i = i + 1) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(BUFFER + ((WIDTH + BUFFER) * i), BUFFER, WIDTH, WIDTH)];
[view setBackgroundColor:[colorArray objectAtIndex:i]];
[[self ViewArray] addObject:view];
[[self view] addSubview:view];
}
}
- (IBAction)hideViews:(id)sender {
for (int i = 0; i < [[self ViewArray] count]; i = i + 1) {
if (i == 1 || i == 4) {
[(UIView *)[[self ViewArray] objectAtIndex:i] setHidden:YES];
}
}
[self centerVisibleViews];
}
- (void)centerVisibleViews {
int visibleViews = 0;
for (int i = 0; i < [[self ViewArray] count]; i = i + 1) {
if (![(UIView *)[[self ViewArray] objectAtIndex:i] isHidden]) {
visibleViews = visibleViews + 1;
}
}
float totalWidth = (BUFFER * 2) + (WIDTH * [[self ViewArray] count]) + (BUFFER * ([[self ViewArray] count] - 1));
float visibleWidth = (BUFFER * 2) + (WIDTH * visibleViews) + (BUFFER * (visibleViews - 1));
// Make it sexy with an animation!!!
[UIView animateWithDuration:1.0
animations:^{
float offset = (totalWidth - visibleWidth) / 2.0;
for (int i = 0; i < [[self ViewArray] count]; i = i + 1) {
UIView *thisView = [[self ViewArray] objectAtIndex:i];
if (![thisView isHidden]) {
[thisView setFrame:CGRectMake(offset, BUFFER, WIDTH, WIDTH)];
offset = offset + WIDTH + BUFFER;
}
}
}
];
}
@end
唯一要做的就是在主视图中添加一个按钮并将其绑定到IBOutlet
。在我的iPad模拟器上很漂亮。
答案 1 :(得分:2)
修改@ mbm30075代码后的最终解决方案如下:
- (void)centerVisibleViews{
int visibleViews = 0;
for (int i = 0; i < [[self imgViews] count]; i = i + 1) {
if (![(UIView *)[[self imgViews] objectAtIndex:i] isHidden]) {
visibleViews = visibleViews + 1;
}
}
if(visibleViews==0) return;
float screenWidth = imgAnimals.superview.bounds.size.width;
//float maxWidthImage = screenWidth / visibleViews;
float imgWidth = [imgAnimals frame].size.width;
// Make it sexy with an animation!!!
[UIView animateWithDuration:1.0
animations:^{
float offset = screenWidth / 2.0 - (visibleViews * imgWidth / 2.0);
for (int i = 0; i < [[self imgViews] count]; i = i + 1) {
UIView *thisView = [[self imgViews] objectAtIndex:i];
if (![thisView isHidden]) {
CGRect rect = [thisView frame];
rect.origin = CGPointMake(offset, 0);
[thisView setFrame:rect];
offset = offset + rect.size.width;
}
}
}
];
}
问题是我想使用我分配给UIImageViews的特定宽度,并将所有宽度放在中间。我希望这对任何人都有帮助!
非常感谢!