两个文件 (.h / .m) 有用的组件和实用类,将它们轻松地集成到您的 iOS 项目中。
许可证:MIT (免费使用,无需署名)。
关注我们的推特: @SoloComponents。
注意:我想收集尽可能多的开源组件,而不仅仅是发布我自己的。如果您有有用的 iOS 类且不依赖于任何东西,请自由地分叉、添加并向我发送补丁请求!
ATPagingView 是围绕 UIScrollView (水平) 分页模式的包装,其 API 类似于 UITableView。
状态:生产可用,已至少被一个应用商店应用使用。
您将实现两种代理方法来提供页面视图
- (NSInteger)numberOfPagesInPagingView:(ATPagingView *)pagingView {
return 10;
}
- (UIView *)viewForPageInPagingView:(ATPagingView *)pagingView atIndex:(NSInteger)index {
UIView *view = [pagingView dequeueReusablePage];
if (view == nil) {
view = [[[DemoPageView alloc] init] autorelease];
}
return view;
}
当用户在页面间导航时,您也会被通知
- (void)currentPageDidChangeInPagingView:(ATPagingView *)pagingView {
self.navigationItem.title = [NSString stringWithFormat:@"%d of %d", pagingView.currentPageIndex+1, pagingView.pageCount];
}
您可以直接使用 ATPagingView 或从 ATPagingViewController 衍生视图控制器。
ATPagingViewController 类似于 UITableViewController 并
loadView
自动创建 ATPagingView,viewWillAppear:
中调用 reloadData
,如果您想要不使用 ATPagingViewController 而使用 ATPagingView,则需要进行以下操作
delegate
属性提供您的代理对象,reloadData
来填充视图,如果想要支持旋转,则需要从您的视图控制器中调用 willAnimateRotation
和 didRotate
方法
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self.pagingView willAnimateRotation];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self.pagingView didRotate];
}
与 Photos.app 中缩略图屏幕类似,按照行和列排列其项的容器,其 API 设计灵感来源于 UITableView。
状态:测试版。
享受熟悉的代理方法
- (NSInteger)numberOfItemsInArrayView:(ATArrayView *)arrayView {
return 97;
}
- (UIView *)viewForItemInArrayView:(ATArrayView *)arrayView atIndex:(NSInteger)index {
DemoItemView *itemView = (DemoItemView *) [arrayView dequeueReusableItem];
if (itemView == nil) {
itemView = [[[DemoItemView alloc] init] autorelease];
}
return itemView;
}
有 ATArrayViewController,它可以进一步减少您需要编写的模板代码量。类似于 UITableViewController,它会
loadView
以自动创建 ATArrayView,viewWillAppear:
中数组视图为空,则调用 reloadData
。允许您轻松使用由 malloc 分配的内存块支持的投资图(CGImageRef)。这意味着您可以直接读取或操作图像字节。
状态:准备投入使用。
使用ATByteImage
ATByteImage *blurred = [[ATByteImage alloc] initWithSize:blurredSize];
[blurred clear];
ATByteImageContext *blurredContext = [blurred newContext];
CGContextSetBlendMode(blurredContext.CGContext, kCGBlendModeNormal);
... draw using blurredContext.CGContext ...
[blurredContext release];
UIImage *myOverlay = [blurred extractImage];
以下是一个示例。以下函数在后台图像加载代码中非常有用
// Returns an uncompressed (decoded) UIImage, optimized for drawing speed.
//
// This is a middle ground between [UIImage imageNamed:] and a plain
// [UIImage imageWithContentsOfFile:], as follows:
//
// * [UIImage imageWithContentsOfFile:] loads image data from disk and
// decodes it each time you display the image.
//
// If you are using CATiledLayer to display a large image (and you should,
// since UIImageView is not recommended for images bigger than ~1024x1024),
// the whole JPEG will decoded for EACH tile you display.
//
// * [UIImage imageNamed:@"xxx"] only ever decodes the image once, just as you
// wanted. However it also caches the image and seems to sometimes (always?)
// not release the data even after you release your UIImage.
//
// An app that loads several large images via 'imageNamed' will thus crash
// quite soon with unfamous "error 0".
//
// Another undesired quality of 'imageNamed' is that the image is loaded and
// decoded when it is displayed for the first time, which means you can't
// really do the decoding in a background thread.
//
// * DecompressUIImage([UIImage imageWithContentsOfFile:@"xx.jpg"]) is the
// sweet spot between the two — it returns a fully decoded image which can
// be displayed quickly, and memory management is entirely up to you.
//
UIImage *DecompressUIImage(UIImage *image) {
ATByteImage *byteImage = [[[ATByteImage alloc] initWithImage:image] autorelease];
return [byteImage extractImage];
}