我想建立一个看起来像NewsStand应用程序中的书架的视图...
这里有做这个工作或类似观点的现成的开源项目吗?

编辑:书报摊:

最佳答案

具有单个书架的模式UIImage的AQGridView。该示例是一个跳板,因此必须对源单元进行修改。如果您允许我花几分钟时间查找我的计算机,我很乐意发布应该有所帮助的源代码。

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

导航到名为“Image Demo”的项目,然后导航至另一个名为“Springboard”的项目。您需要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的黄金平均值。

关于iphone - 如何使外观看起来像书报摊书架,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8668256/

10-08 20:49