如何使视图看起来像报摊书架

时间:2011-12-29 13:06:39

标签: iphone ios xcode cocoa-touch

我想在NewsStand应用程序中构建一个看起来像书架的视图...... 在这里是否有任何现成的开源项目来完成这项工作或类似的观点?

编辑:报摊:

enter image description here

6 个答案:

答案 0 :(得分:9)

AQGridView具有单个书架的模式UIImage。该示例是跳板,因此需要修改源单元。如果你允许我花几分钟时间找到我的电脑,我很乐意发布应该有用的源代码。

编辑:从Github下载AQGridView源并将文件解压缩到您选择的目录中。

导航到名为“Image Demo”的项目和另一个名为“Springboard”的项目。您需要跳板项目。复制除项目中预先存在的项目之外的所有项目(因此请排除main.m和前缀)。

重要的文件集是SpringboardIconCell类。从这里,您可以修改单元格。这就是我如何摆脱它:

//.h

#import <UIKit/UIKit.h>  
#import "AQGridViewCell.h"
#import <Foundation/Foundation.h>

@interface SpringBoardIconCell : AQGridViewCell.    
{
UIImageView * _iconView;
UILabel * _title;
UILabel * _titleTwo;
}

@property (nonatomic, retain) UIImage * icon;
@property (nonatomic, copy) NSString * title;
@property (nonatomic, retain) NSString * titleTwo;

//.m
#import "SpringBoardIconCell.h"

#import <QuartzCore/QuartzCore.h>

@implementation SpringBoardIconCell

- (id) initWithFrame: (CGRect) frame reuseIdentifier:(NSString *) reuseIdentifier
{
self = [super initWithFrame: frame reuseIdentifier: reuseIdentifier];
if ( self == nil )
    return ( nil );

UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0.0, 0.0, 155.0, 250.0)
                                                 cornerRadius: 18.0];

_iconView = [[UIImageView alloc] initWithFrame: CGRectMake(0.0, 0.0, 155.0, 250.0)];
_iconView.backgroundColor = [UIColor clearColor];
_iconView.opaque = NO;
_iconView.layer.shadowPath = path.CGPath;
_iconView.layer.shadowRadius = 0.0;
_iconView.layer.shadowOpacity = 0.0;
_iconView.layer.shadowOffset = CGSizeMake( 20.0, 20.0 );

_title = [[UILabel alloc] initWithFrame: CGRectZero];
_title.highlightedTextColor = [UIColor blackColor];
_title.font = [UIFont boldSystemFontOfSize: 12.0];
_title.adjustsFontSizeToFitWidth = YES;
_title.minimumFontSize = 12.0;
_title.backgroundColor = [UIColor clearColor];
_title.textColor = [UIColor whiteColor];
_title.textAlignment = UITextAlignmentCenter;
_title.numberOfLines = 1;

_titleTwo = [[UILabel alloc] initWithFrame: CGRectZero];
_titleTwo.highlightedTextColor = [UIColor blackColor];
_titleTwo.font = [UIFont fontWithName:@"AmericanTypewriter" size:20];
_titleTwo.adjustsFontSizeToFitWidth = YES;
_titleTwo.minimumFontSize = 18.0;
_titleTwo.backgroundColor = [UIColor clearColor];
_titleTwo.textColor = [UIColor blackColor];
_titleTwo.textAlignment = UITextAlignmentRight;
_titleTwo.numberOfLines = 4;

[self.contentView addSubview: _iconView];
[_iconView addSubview: _title];
[self.contentView addSubview:_titleTwo];
[self.contentView bringSubviewToFront:_titleTwo];

self.contentView.backgroundColor = [UIColor clearColor];
self.backgroundColor = [UIColor clearColor];

self.contentView.opaque = NO;
self.opaque = NO;

self.selectionStyle = AQGridViewCellSelectionStyleNone;

return ( self );
}

- (void) dealloc
{
[_title release];
[_iconView release];
[super dealloc];
}

- (UIImage *) icon
{
return ( _iconView.image );
}

- (void) setIcon: (UIImage *) anIcon
{
_iconView.image = anIcon;
[self setNeedsLayout];

}
- (CALayer *) glowSelectionLayer
{
return ( _iconView.layer );
}
- (NSString *) title
{
return ( _title.text );
}

- (NSString*)titleTwo {
return (_titleTwo.text);
}

- (void) setTitle: (NSString *) title
{
_title.text = title;
[self setNeedsLayout];
}
-(void)setTitleTwo:(NSString *)titleTwo {
_titleTwo.text = titleTwo;
[self setNeedsLayout];
}
- (void) layoutSubviews
{
[super layoutSubviews];

[_titleTwo setFrame:CGRectMake(self.contentView.frame.origin.x + 45, 10, 135, 100.0f)];

CGSize imageSize = _iconView.image.size;
CGRect bounds = CGRectInset( self.contentView.bounds, 10.0, 10.0 );

[_title sizeToFit];
CGRect frame = _title.frame;
frame.size.width = 155.0;
frame.origin.y = CGRectGetMaxY(bounds) - frame.size.height;
frame.origin.x = 0;
_title.frame = frame;

// adjust the frame down for the image layout calculation
bounds.size.height = frame.origin.y - bounds.origin.y;

if ( (imageSize.width <= bounds.size.width) &&
    (imageSize.height <= bounds.size.height) )
{
    return;
}

// scale it down to fit
CGFloat hRatio = bounds.size.width / imageSize.width;
CGFloat vRatio = bounds.size.height / imageSize.height;
CGFloat ratio = MIN(hRatio, vRatio);

[_iconView sizeToFit];
frame = _iconView.frame;
frame.size.width = floorf(imageSize.width * ratio);
frame.size.height = floorf(imageSize.height * ratio);
frame.origin.x = floorf((bounds.size.width - frame.size.width) * 0.5);
frame.origin.y = floorf((bounds.size.height - frame.size.height) * 0.5);
_iconView.frame = frame;
}
@end

现在进入主视图或书架所在的位置。您需要采用AQGridView委托和数据源

<AQGridViewDelegate, AQGridViewDataSource>

然后是委托方法

- (NSUInteger) numberOfItemsInGridView: (AQGridView *) gridView
{
return (_array.count);
} 
- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
static NSString * EmptyIdentifier = @"EmptyIdentifier";
static NSString * CellIdentifier = @"CellIdentifier";

if ( index == _emptyCellIndex )
{
    NSLog( @"Loading empty cell at index %u", index );
    AQGridViewCell * hiddenCell = [gridView dequeueReusableCellWithIdentifier: EmptyIdentifier];
    if ( hiddenCell == nil )
    {
        // must be the SAME SIZE AS THE OTHERS
        // Yes, this is probably a bug. Sigh. Look at -[AQGridView fixCellsFromAnimation] to fix
        hiddenCell = [[[AQGridViewCell alloc] initWithFrame: CGRectMake(0.0, 0.0, 155.0, 250.0)
                                            reuseIdentifier: EmptyIdentifier] autorelease];
    }
    hiddenCell.hidden = YES;
    return ( hiddenCell );
}

SpringBoardIconCell * cell = (SpringBoardIconCell *)[gridView dequeueReusableCellWithIdentifier: CellIdentifier];
if ( cell == nil )
{
    cell = [[[SpringBoardIconCell alloc] initWithFrame: CGRectMake(0.0, 0.0, 250.0, 250.0) reuseIdentifier: CellIdentifier] autorelease];
}    
    UIImage * image = [UIImage imageNamed:@"Image.png"];
cell.icon = image;

return ( cell );
}

- (CGSize) portraitGridCellSizeForGridView: (AQGridView *) gridView
{
return ( CGSizeMake(250.0, 250.0) );
}

但是当说完所有内容时,你需要一个书架,所以从互联网上获取书架图像。然后,将其裁剪下来,只显示一层架子。在AQGridview中,带有图案图像的背景将完美地工作,因此在viewDidLoad中,添加:

 _gridView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"PatternImage.png"]];

结果产生一个250 X 250的单元格,其中包含一个单元格标题视图和一个底部标题视图。但是,单元格的图标是155 X 250的黄金平均数。

答案 1 :(得分:3)

我不知道任何开源项目,但您可以使用背景图片作为报刊亭框架本身。然后在上面你可以放置一个带有“架子”背景图像的UIScrollView。从那里我想你可以将各个新闻项目添加为UIButtons或作为表格视图的一部分。

答案 2 :(得分:2)

为此,您可以将tableview保存在Newstand标题和按钮的标题视图中,每个单元格将包含一本书。本书将是一个UIButton,您必须将单个机架图像设置为单元格的背景。创建自定义单元格将是有益的。我不喜欢滚动视图,因为你需要管理每本书的坐标,如果你将背景保持为包含许多机架的单个图像,那么它看起来就像你的书架漂浮在机架上。

https://github.com/AlanQuatermain/AQGridView

如果你愿意的话试试这个。我从中获取了逻辑并实现了自己的网格。

答案 3 :(得分:1)

带有预渲染书架背景的UITable / ScrollView?

答案 4 :(得分:0)

只需在后台使用书架图片。

答案 5 :(得分:0)

只需使用UIView(具有书架等背景)和UITable(使用自定义(修改)单元格用于货架图像)。你不需要使用任何现成的非常简单的从头开始构建。