iOS - 在 iCarousel 中添加自定义 UIView
Posted
技术标签:
【中文标题】iOS - 在 iCarousel 中添加自定义 UIView【英文标题】:iOS - Add custom UIView in the iCarousel 【发布时间】:2012-09-09 20:44:22 【问题描述】:我正在尝试将自定义 UIViews(使用 Core Plot 绘制的图表)添加为 iCarousel 中的元素,但我没有看到正在绘制的图表。这是我的头文件:
@interface MTViewController : UIViewController <iCarouselDataSource, iCarouselDelegate,
CPTBarPlotDataSource, CPTBarPlotDelegate>
@property (strong, nonatomic) IBOutlet iCarousel *aCarousel;
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (nonatomic) BOOL wrap;
@property (strong, nonatomic) IBOutlet CPTGraphHostingView *graphView;
@property (nonatomic, retain) NSMutableArray *data;
@property (strong, nonatomic) NSMutableArray *animals;
@property (nonatomic, retain) CPTXYGraph *graph;
@property (strong, nonatomic) NSMutableArray *graphs;
@property (strong, nonatomic) NSMutableArray *descriptions;
@end
实现文件是:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
//set up carousel data
wrap = NO;
self.graphs = [NSMutableArray arrayWithObjects: self.graphView,nil];
return self;
- (void)viewDidLoad
aCarousel.type = iCarouselTypeCoverFlow2;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
- (void)viewDidUnload
[self setACarousel:nil];
[self setLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
#pragma - mark iCarousel Delegate
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
return [self.graphs count];
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
// limit the number of item views loaded concurrently (for performance)
return 1;
- (CPTGraphHostingView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(CPTGraphHostingView *)view
// create a numbered view
// view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[animals objectAtIndex:index]]];
// return view;
CGSize size = self.graphView.frame.size;
float width = size.width;
CPTGraphHostingView *currentGraph ;
for (int i = 0; i < self.graphs.count; i++)
CGRect frame;
frame.origin.x = width * i;
frame.origin.y = 0;
frame.size = size;
currentGraph = [graphs objectAtIndex:i];
currentGraph.frame = frame;
[self generateBarPlot:frame];
return currentGraph;
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
return 1;
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
// usually this should be slightly wider than the item views
return 240;
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
return self.wrap;
- (void)carouselDidScroll:(iCarousel *)carousel
//[label setText:[NSString stringWithFormat:@"%@", [descriptions objectAtIndex:carousel.currentItemIndex]]];
[label setText:[NSString stringWithFormat:@"Testing"]];
....后面是生成图的方法
任何帮助将不胜感激。
感谢阅读。
【问题讨论】:
我已经尝试将 iCarousel.h 文件中的协议方法更改为 CPTGraphHostingView,但没有得到它的工作。 你检查框架的大小了吗? 另外,看起来很奇怪。看起来 graphView 是加载视图的一部分,但随后您尝试在轮播中使用该视图。这似乎很奇怪。原始 graphView 来自哪里,您是否尝试过验证它是否单独正确显示? 原始的graphView得到正确显示。我在 ScrollView 中添加了两个 graphView,效果很好。 如果你想看看,我可以发布更多代码。 【参考方案1】:我通过将自定义视图转换为 UIView 解决了这个问题。
【讨论】:
你做了其他事情。铸造不会改变对象的类型..例如你可以把苹果扔进草莓里——>但事实并非如此。 我的意思是 iCarousel 采用 UIView 或其任何子类,因此我能够将核心绘图图放在那里。以上是关于iOS - 在 iCarousel 中添加自定义 UIView的主要内容,如果未能解决你的问题,请参考以下文章
将带有自动布局的自定义 UIView 添加到 iCarousel