我想開發一個庫,它需要一個圖像數組,並返回一個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