测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可协议 | MIT |
发布最后发布 | 2016年3月 |
由 Matteo Gobbi,Daniele Bogo 维护。
一个漂亮的 viewController,具有类似于 Spotify 应用程序的特效,如 tableView 和惊人的特效。使用 MGSpotyViewController,您可以实现几种布局,如下所示
此代码必须在 ARC 下使用。如果您的代码不使用 ARC,您可以使用编译器标志 -fobjc-arc
将此源代码标记为 仅限 ARC
在包中包含了一个使用此类的示例。
最好的方法是扩展 MGSpotyViewController
。在包中,您可以看到 MGViewController.{h,m}
作为示例。
这里有一些解释
初始化很简单。您只需传递主图像用于模糊效果即可
MGViewController *spotyViewController = [[MGViewController alloc] initWithMainImage:[UIImage imageNamed:@"example"]];
MGViewController
扩展了 MGSpotyViewController
//
// MGViewController.h
// MGSpotyView
//
// Created by Matteo Gobbi on 25/06/2014.
// Copyright (c) 2014 Matteo Gobbi. All rights reserved.
//
#import "MGSpotyViewController.h"
@interface MGViewController : MGSpotyViewController
@end
设置 MGSpotyViewController
的 delegate
和 dataSource
- (instancetype)init
{
if (self = [super init]) {
self.dataSource = myDataSource; //Or self
self.delegate = myDelegate; //Or self
}
return self;
}
在实现文件中,首先应设置 overView
。基本上,overView
是 保持在上模糊图像上的标题视图
- (void)viewDidLoad {
[self setOverView:self.myOverView];
}
//This is just an example view created by code, but you can return any type of view.
- (UIView *)myOverView {
UIView *view = [[UIView alloc] initWithFrame:self.overView.bounds];
//Add an example imageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(view.center.x-50.0, view.center.y-60.0, 100.0, 100.0)];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageView setClipsToBounds:YES];
[imageView setImage:[UIImage imageNamed:@"example"]];
[imageView.layer setBorderColor:[UIColor whiteColor].CGColor];
[imageView.layer setBorderWidth:2.0];
[imageView.layer setCornerRadius:imageView.frame.size.width/2.0];
//Add an example label
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(view.center.x-120.0, view.center.y+50.0, 240.0, 50.0)];
[lblTitle setText:@"Name Surname"];
[lblTitle setFont:[UIFont boldSystemFontOfSize:25.0]];
[lblTitle setTextAlignment:NSTextAlignmentCenter];
[lblTitle setTextColor:[UIColor whiteColor]];
[view addSubview:imageView];
[view addSubview:lblTitle];
return view;
}
还有一件事需要配置,即为 tableView
。tableView
已经在 MGSpotyViewController
中,您只需设置 MGSpotyViewControllerDataSource
和 MGSpotyViewControllerDelegate
并使用其方法。
您必须记住,区域 0 是保留的,因此即使您返回一个区域,您的代理也会收到编号为 1 的区域的调用,而不是 0。基本上,对于区域,计数器从 1 而不是从 0 开始。
#pragma mark - MGSpotyViewControllerDataSource
- (NSInteger)numberOfSectionsInSpotyViewController:(MGSpotyViewController *)spotyViewController
{
return 1;
}
- (NSInteger)spotyViewController:(MGSpotyViewController *)spotyViewController
numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)spotyViewController:(MGSpotyViewController *)spotyViewController
tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// IndexPath is 1-1, 1-2, 1-3 etc.
UITableViewCell *cell = // Your cell initialisation
return cell;
}
如果您需要管理区域标题或区域标题视图
#pragma mark - MGSpotyViewControllerDelegate
- (CGFloat)spotyViewController:(MGSpotyViewController *)spotyViewController
heightForHeaderInSection:(NSInteger)section
{
return 20.0;
}
- (NSString *)spotyViewController:(MGSpotyViewController *)spotyViewController
titleForHeaderInSection:(NSInteger)section
{
return @"My Section";
}
有很多属性您可以与之交互,以获得满足您的需求的最佳结果
/**
* The tint color on top of the blur. When alpha is 1.0
* you'll not be able to see the image behind.
*/
@property (nonatomic, strong) UIColor *tintColor;
/**
* Indicate if the overView has to fade out when scrolling up
* Default value: NO
*/
@property (atomic) BOOL overViewFadeOut;
/**
* Indicate if the main image has to get unblurred when scrolling down
* Default value: YES
*/
@property (atomic) BOOL shouldUnblur;
/**
* Indicate if the overView height is resized automatically when the
* device is rotated (ie when the height of the interface change)
* Default value: YES
*/
@property (atomic) BOOL flexibleOverviewHeight;
/**
* Set the value of the blur radius.
* Default value: 20.0
*/
@property (nonatomic) CGFloat blurRadius;
还有一个初始化器,它接受一个滚动类型作为输入,这是您在之前的示例中看到的基本滚动类型
- (instancetype)initWithMainImage:(UIImage *)image tableScrollingType:(MGSpotyViewTableScrollingType)scrollingType;
Matteo Gobbi
MGSpotyViewController 可在 MIT 许可协议下获得。