搜索菜单栏侧滑效果控件SearchView

本人视频教程系类   iOS中CALayer的使用

效果1:

效果2:

项目中用到的图片 bgImg@2x.png:

源码:

SearchView.h + SearchView.m

//
// SearchView.h
// SearchView
//
// Created by YouXianMing on 14/12/25.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> #define SearchView_ScreenWidth [[UIScreen mainScreen] bounds].size.width
#define SearchView_ScreenHeight [[UIScreen mainScreen] bounds].size.height @protocol SearchViewDelegate <NSObject>
@optional
- (void)searchViewTapEventString:(NSString *)string;
@end @interface SearchView : UIView @property (nonatomic, weak) id<SearchViewDelegate> delegate; @property (nonatomic, assign) CGRect startFrame;
@property (nonatomic, assign) CGRect endFrame; @property (nonatomic, strong) NSArray *dataStringArray; // 数据源数组 - (void)expendCellAnimated:(BOOL)animated; @end
//
// SearchView.m
// SearchView
//
// Created by YouXianMing on 14/12/25.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "SearchView.h"
#import "SearchViewCell.h" static NSString *searchViewCell = @"searchViewCell"; @interface SearchView ()<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *stringArray; // 数据源 @end @implementation SearchView - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 初始化TableView
self.tableView = [[UITableView alloc] initWithFrame:self.bounds
style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.75]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableView registerClass:[SearchViewCell class]
forCellReuseIdentifier:searchViewCell];
[self addSubview:self.tableView];
}
return self;
} #pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.stringArray.count + ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:searchViewCell]; if (indexPath.row == ) {
cell.textLabel.text = @"search...";
cell.textLabel.textColor = [UIColor colorWithRed:0.780 green:0.800 blue:0.812 alpha:];
cell.textLabel.font = [UIFont italicSystemFontOfSize:.f];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
cell.showLine = YES; return cell;
} // 获取文本
cell.textLabel.text = self.stringArray[indexPath.row - ];
cell.textLabel.textColor = [UIColor yellowColor];
cell.backgroundColor = [UIColor clearColor]; return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return .f;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == ) {
if (_delegate && [_delegate respondsToSelector:@selector(searchViewTapEventString:)]) {
[_delegate searchViewTapEventString:@"搜索"];
} } else {
if (_delegate && [_delegate respondsToSelector:@selector(searchViewTapEventString:)]) {
[_delegate searchViewTapEventString:self.stringArray[indexPath.row - ]];
}
} UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
}
- (void)expendCellAnimated:(BOOL)animated {
// 如果为空,则直接返回
if (self.dataStringArray == nil) {
return;
} self.stringArray = [NSArray arrayWithArray:self.dataStringArray]; if (animated) {
NSMutableArray *indexPathArray = [NSMutableArray array]; for (int i = ; i < self.stringArray.count; i++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i+ inSection:];
[indexPathArray addObject:path];
} [self.tableView insertRowsAtIndexPaths:indexPathArray
withRowAnimation:UITableViewRowAnimationTop];
} else {
[self.tableView reloadData];
}
} #pragma mark - 重写setter,getter方法
@synthesize startFrame = _startFrame;
- (void)setStartFrame:(CGRect)startFrame {
_startFrame = startFrame;
self.frame = startFrame;
}
- (CGRect)startFrame {
return _startFrame;
} @end

SearchViewCell.h + SearchViewCell.m

//
// SearchViewCell.h
// SearchView
//
// Created by YouXianMing on 14/12/25.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface SearchViewCell : UITableViewCell @property (nonatomic, assign) BOOL showLine; @end
//
// SearchViewCell.m
// SearchView
//
// Created by YouXianMing on 14/12/25.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "SearchViewCell.h" @interface SearchViewCell () @property (nonatomic, strong) UIView *line; @end @implementation SearchViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.selectedBackgroundView = [self createSelectedBackgroundView]; // 线条view
_line = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
_line.backgroundColor = [UIColor redColor];
_line.alpha = ;
[self addSubview:_line];
} return self;
} - (UIView *)createSelectedBackgroundView {
UIView *selectedView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
selectedView.backgroundColor = [UIColor purpleColor]; return selectedView;
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
} @synthesize showLine = _showLine;
- (void)setShowLine:(BOOL)showLine {
_showLine = showLine;
if (showLine == YES) {
_line.alpha = .f;
} else {
_line.alpha = .f;
}
}
- (BOOL)showLine {
return _showLine;
} @end

控制器源码:

//
// ViewController.m
// SearchView
//
// Created by YouXianMing on 14/12/25.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "SearchView.h" /**
枚举值
*/
typedef enum : NSUInteger {
UIVIEW_BackgroundView = 0x19871220,
UIVIEW_SearchView,
} EnumViewController; @interface ViewController ()<SearchViewDelegate> @property (nonatomic, strong) SearchView *searchView; @end #define ScreenWidth [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 设置背景图
self.view.layer.contents = (__bridge id)([UIImage imageNamed:@"bgImg"].CGImage); // 隐藏状态栏
[UIApplication sharedApplication].statusBarHidden = YES; // 按钮
UIButton *button = [[UIButton alloc] initWithFrame:self.view.bounds];
[self.view addSubview:button];
[button addTarget:self
action:@selector(buttonEvent:)
forControlEvents:UIControlEventTouchUpInside];
} /**
* 代理方法
*
* @param string 被点击的字符串
*/
- (void)searchViewTapEventString:(NSString *)string {
NSLog(@"%@", string);
} /**
* 创建出SearchView
*
* @param duration 动画执行时间
*/
- (void)createSearchViewWithAnimatedDuration:(CGFloat)duration { SearchView *searchView = \
[[SearchView alloc] initWithFrame:CGRectMake(ScreenWidth, , ScreenWidth, ScreenHeight)];
searchView.alpha = .f;
searchView.delegate = self;
searchView.tag = UIVIEW_SearchView;
searchView.dataStringArray = @[@"YouXianMing", @"Soup", @"Y.X.M.", @"Play Game",
@"WaKaLiMaXiDa.", @"E.T.", @"Q.L.", @"SLG"];
[self.view addSubview:searchView];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(, , , ScreenHeight)];
lineView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5f];
[searchView addSubview:lineView]; [UIView animateWithDuration:duration
delay:.f
usingSpringWithDamping:.f
initialSpringVelocity:.f
options:UIViewAnimationOptionLayoutSubviews
animations:^{
searchView.alpha = .f;
searchView.frame = CGRectMake(, , ScreenWidth, ScreenHeight);
}
completion:^(BOOL finished) {
[searchView expendCellAnimated:YES];
}];
} /**
* 移除SearchView
*
* @param duration 动画执行时间
*/
- (void)removeSearchViewWithAnimatedDuration:(CGFloat)duration {
UIView *searchView = [self.view viewWithTag:UIVIEW_SearchView]; [UIView animateWithDuration:duration animations:^{
searchView.frame = CGRectMake(ScreenWidth, , ScreenWidth, ScreenHeight);
searchView.alpha = .f;
} completion:^(BOOL finished) {
[searchView removeFromSuperview];
}];
} /**
* 创建出背景View
*
* @param duration 动画执行时间
*/
- (void)createBackgroundViewWithAnimatedDuration:(CGFloat)duration {
UIView *backedgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
backedgroundView.backgroundColor = [UIColor blackColor];
backedgroundView.tag = UIVIEW_BackgroundView;
backedgroundView.alpha = .f;
backedgroundView.userInteractionEnabled = NO;
[self.view addSubview:backedgroundView]; [UIView animateWithDuration:duration animations:^{
backedgroundView.alpha = 0.35;
}];
} /**
* 移除背景View
*
* @param duration 动画执行时间
*/
- (void)removeBackgroundViewWithAnimatedDuration:(CGFloat)duration {
UIView *backedgroundView = [self.view viewWithTag:UIVIEW_BackgroundView]; [UIView animateWithDuration:duration animations:^{
backedgroundView.alpha = .f;
} completion:^(BOOL finished) {
[backedgroundView removeFromSuperview];
}];
} /**
* 按钮点击事件
*
* @param button 按钮
*/
- (void)buttonEvent:(UIButton *)button {
if (button.selected == NO) {
button.selected = YES;
[self createBackgroundViewWithAnimatedDuration:0.45];
[self createSearchViewWithAnimatedDuration:0.45];
} else {
button.selected = NO;
[self removeBackgroundViewWithAnimatedDuration:0.45];
[self removeSearchViewWithAnimatedDuration:0.45];
}
} @end

以下是需要注意的一些小地方:

搜索菜单栏侧滑效果控件SearchView的更多相关文章

  1. Android 自定义View修炼-打造完美的自定义侧滑菜单/侧滑View控件

    一.概述 在App中,经常会出现侧滑菜单,侧滑滑出View等效果,虽然说Android有很多第三方开源库,但是实际上 咱们可以自己也写一个自定义的侧滑View控件,其实不难,主要涉及到以下几个要点: ...

  2. Android 自定义支持快速搜索筛选的选择控件(一)

    Android 自定义支持快速搜索筛选的选择控件 项目中遇到选择控件选项过多,需要快速查找匹配的情况. 做了简单的Demo,效果图如下: 源码地址:https://github.com/whieenz ...

  3. Android 打造完美的侧滑菜单/侧滑View控件

    概述 Android 打造完美的侧滑菜单/侧滑View控件,完全自定义实现,支持左右两个方向弹出,代码高度简洁流畅,兼容性高,控件实用方便. 详细 代码下载:http://www.demodashi. ...

  4. ASwipeLayout一个强大的侧滑菜单控件

    Android中侧滑的场景有很大,大部分是基于RecyclerView,但是有些时候你可以动态地addView到一个布局当中,也希望它实现侧滑,所以就产生了ASwipeLayout,该控件不仅支持在R ...

  5. Android打造完美的刮刮乐效果控件

    技术:Android+Java   概述 趁着元旦假期之际,首先在这里,我祝福大家在新的2019年都一个个的新健康,新收入,新顺利,新如意!!! 上一偏,我介绍了用Xfermode实现自定义圆角和椭圆 ...

  6. Android改进版CoverFlow效果控件

    最近研究了一下如何在Android上实现CoverFlow效果的控件,其实早在2010年,就有Neil Davies开发并开源出了这个控件,Neil大神的这篇博客地址http://www.inter- ...

  7. 009 Android Fragment动态用法(实现动态菜单栏)设置控件为满屏状态

    ·1.MainActivity采用线性布局 2.在app--->res--->layout,右击new--->xml---->layout xml FILE <1> ...

  8. js搜索相同类型的控件全选、取值(Checkbox)

    function selectAll(obj) { if (obj.checked) { $("input[type='checkbox']").each(function () ...

  9. 8.效果控件之移动&&缩放

    1.移动应用(横摇.竖摇) 1.移动应用(横向缩小跟踪排列) 1.移动应用(自由缩小跟踪排列)

随机推荐

  1. openTSDB(转)

    1.OpenTSDB介绍 1.1.OpenTSDB是什么?主要用途是什么? 官方文档这样描述:OpenTSDB is a distributed, scalable Time Series Datab ...

  2. cookie和session的区别,session的生命周期,

    这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪 ...

  3. linux 查找删除找定文件

    find . -name "*.lastUpdated" -exec rm -rf {} \; 这个命令是find的基本用法,可以分两部分,find ~/ -name " ...

  4. java面试⑥框架部分

    2.5.1 什么是框架: 2.5.2 MVC模式 2.5.3 MVC框架 2.5.4 简单讲一下struts2的执行流程 2.5.5 Struts2中的拦截器,你都用它干什么? 2.5.6 简单讲一下 ...

  5. ABP实战--项目结构

    学习完毕With ASP.NET Core & Entity Framework Core Part-1及Part-2后,只实现了基本的功能,使用该工程继续学习ABP的更多功能. 更改项目结构 ...

  6. https在电子邮件安全解决方案

    电子邮件安全解决方案 电子邮件已经成为现代人最重要和最不可缺少的个人生活和工作的通信工具之一,特别是企业应用.但是,您也许不知道,所有电子邮件系统都是明文传输,也就是说:您的每一个重要邮件都是在以“明 ...

  7. [转] What is a Full Stack developer?

    期望一个凡人掌握开发过程中各个方面的知识,合理吗?也许不合理,但是Facebook正是要寻找这样的人.在一个OSCON会议上,一名Facebook的工程师告诉我的,他们只聘请“全能(Full stac ...

  8. c#基础学习(0806)之StringBuilder的使用

    以前字符串的拼接基本都是用string来完成的,从来没有考虑过性能或者速度的问题,自从学习了StringBuilder之后才发现两者的差距有多大,当然,数据量比较小的时候,用string还是挺方便的, ...

  9. HTML的map-area的使用

    使用背景 在把设置图转成页面的时候,时常会遇到这种情况:一张小图片上有好多个可以点击的小图标,按常规的处理方法是把这一个一个的小图切出来,然后每个加个a标签进行跳转,但是这样会非常的浪费时间,而且会增 ...

  10. Java基础教程(19)--Object类

      Object类位于类结构树的最顶端,所有的类都是它的直接或间接子类,因此所有的类都继承了Object类的方法,我们可以在需要的时候覆盖这些方法.下面是一些将会在本文中讨论的Object类的方法: ...