我想开发一个库,它需要一个图像数组,并返回一个scrollView与图像并排。UIImageView没有添加到UIView
但是,当我添加此scrollView,我回到我的主视图,它不会添加图像。图像是正确的。
我的想法是并排使用每个图像的滚动视图,并使用滚动条来显示幻灯片。首先,这个概念好吗?
其次,我不明白为什么我在模拟器中运行应用程序时不显示图像。
需要更多的细节请询问。
代码如下。 我的头文件:
#import <UIKit/UIKit.h>
@interface HCIImageSlideShowView : UIScrollView
-(void) setImages:(NSArray*) imagesArray;
-(void) setBounds:(CGRect)bounds;
-(void) setCaptions:(NSArray*) imageCaptionsArray;
-(void) isEditable:(BOOL)edit;
-(void) setSlideTime:(int) milliSeconds;
-(void) startSlideShow;
- (id) initWithImages:(NSArray*)imagesArray captionsArray:(NSArray*) captionArray
bounds:(CGRect)bounds slideTime:(int)milliSeconds;
@end
我实现文件:
#import "HCIImageSlideShowView.h"
#import "HCIResultListViewController.h"
@interface HCIImageSlideShowView()
@property (strong,nonatomic) NSMutableArray *imagesArray;
@property BOOL editable;
@property (nonatomic) int slideTime;
@property (strong,nonatomic) NSMutableArray *imageCaptionsArray;
@property CGFloat width;
@end
@implementation HCIImageSlideShowView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id) initWithImages:(NSArray*)imagesArray captionsArray:(NSArray*) captionArray
bounds:(CGRect)bounds slideTime:(int)milliSeconds
{
NSLog([NSString stringWithFormat:@"%f,%f", bounds.size.width , bounds.size.height]);
CGFloat width_t = bounds.size.width;
bounds.size.width = [imagesArray count] * bounds.size.width;
self = [[HCIImageSlideShowView alloc] initWithFrame:bounds];
_width = width_t;
[self setBackgroundColor:[[UIColor alloc] initWithRed:0.2 green:0.1 blue:0.3 alpha:0.4]];
if (self) {
[self setImages:imagesArray];
[self setSlideTime:milliSeconds];
[self setCaptions:captionArray];
[self defaultLoad];
}
self.scrollEnabled = YES;
return self;
}
-(void) defaultLoad
{
NSLog([NSString stringWithFormat:@"%f,%f,%f",_width,self.bounds.size.height,self.bounds.size.width]);
for (int i = 0; i < [_imagesArray count]; i++) {
CGRect imageBounds = CGRectMake(i * _width, self.bounds.size.height, _width, self.bounds.size.height);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageBounds];
[imageView setImage:[HCIResultListViewController resizeImage:_imagesArray[i] withWidth:_width withHeight:self.bounds.size.height]];
NSLog([NSString stringWithFormat:@"%f,%f",imageView.bounds.size.height,imageView.bounds.size.width]);
[self addSubview:imageView];
}
}
-(void) setBounds:(CGRect)bounds
{
self.bounds = bounds;
}
-(void) setImages:(NSArray *)imagesArray
{
_imagesArray = [[NSMutableArray alloc] initWithArray:imagesArray];
}
-(void) setSlideTime:(int)milliSeconds
{
_slideTime = milliSeconds;
}
-(void) startSlideShow
{
}
-(void) isEditable:(BOOL)edit
{
_editable = edit;
}
-(void) setCaptions:(NSArray *)imageCaptionsArray {
_imageCaptionsArray = [[NSMutableArray alloc] initWithArray:imageCaptionsArray];
}
@end
检查你的自我是否得到建立与否,如果(个体经营)条件语句旁边写着setBackgroundColor – Mrunal 2013-03-18 13:40:30
为什么你的init方法中分配一个新HCIImageSlideShowView对象? AFAIK只需调用'self = [super initWithFrame:...];应该就足够了''根据你对'self'返回的内容以及你是否使用IB,这可能是好的。但是你冒险最终得到两个HCIImageSlideShowView,一个设置正确但没有显示,另一个显示为空。 – 2013-03-18 13:42:22
是的。自我正在创造。在屏幕上它实际上显示背景颜色。但是,图像不显示。滚动条都没有。 – 2013-03-18 13:42:34