我正在尝试通过ScrollView添加图像。我在plist文件中有x和y点,它们由枚举调用。
enum {
kLogoImage = 1,
kNewLogoImage,
};
@implementation
- (void) _setupStaticViews
{
UIButton *image = [UIButton buttonWithType:UIButtonTypeCustom];
[image setBackgroundImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];
[image addTarget:self action:@selector(_addContactButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
image.frame = CGRectMake(0.0 ,0.0, 150.0, 150.0);
image.tag = kLogoImage;
[_mapImageView addSubview:image];
_staticViews = @[image];
UIButton *image2 = [UIButton buttonWithType:UIButtonTypeCustom];
[image2 setBackgroundImage:[UIImage imageNamed:@"Time Bakground.png"] forState:UIControlStateNormal];
[image2 addTarget:self action:@selector(_addContactButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
image2.frame = CGRectMake(0.0 ,0.0, 250.0, 150.0);
image2.tag = kNewLogoImage;
[_mapImageView addSubview:image2];
_staticViews = @[image2];
for ( UIView *view in _staticViews ) {
CGPoint point = [self _basePositionForView:view];
CGRect frame = view.frame;
frame.origin = point;
view.frame = frame;
}
}
如果我只显示图像,它可以正常工作。但我需要添加多个并添加第二个导致第一个位于xy点0,0,而Image2不可见。 我该如何添加另一张图片?
- (CGPoint) _basePositionForView:(UIView *)view {
NSString *key = [NSString stringWithFormat:@"%d", view.tag];
NSString *stringValue = [_coordinates objectForKey:key];
NSArray *values = [stringValue componentsSeparatedByString:@":"];
if ( [values count] < 2 ) return CGPointZero;
CGPoint result = CGPointMake([[values objectAtIndex:0] floatValue], [[values objectAtIndex:1] floatValue]);
return result;
}
答案 0 :(得分:1)
我注意到你先做
_staticViews = @[image];
以后
_staticViews = @[image2];
你不是想要拥有
_staticViews = @[image, image2];
答案 1 :(得分:0)
您应该在框架中相应地迭代并更改x:
for ( int iterator=0; iterator< [[_staticViews subviews] count]; iterator++){
...
current_image.frame = CGRectMake(YOUR_IMG_WIDTH * iterator ,0.0, YOUR_IMG_WIDTH, 150.0);
...}