iOS移动View点击事件
2016-06-02 09:35
435 查看
在做开发的时候发现移动中的view不能点击,在网上找了很久之后自己写了一个移动的view点击事件,希望对大家有帮助废话不多说,直接开始。
新建工程,先删除文件中的ViewController.h和ViewController.m文件,新建MoveViewController继承于UIViewController
将MoveViewController作为window的根视图控制器,代码如下
在AppDelegate.m文件导入头文件
#import "MoveViewController.h"
接着在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {}
函数中添加如下代码,并且在Info.plist文件中将Main storyboard file base name关键字段的Main删除
MoveViewController *moveViewVc = [[MoveViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:moveViewVc];
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.rootViewController = nav;
[_window makeKeyAndVisible];
现在根视图控制变成MoveViewController,然后就可以在MoveViewController中进行操作了
在这之前我们需要先封装一个移动视图
新建文件继承于UIView
moveView.h文件代码如下:
//
// moveView.h
// ZQMoveViewClick
//
// Created by 赵前 on 16/6/1.
// Copyright © 2016年 赵前. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface moveView : UIView
/**
移动视图的头部视图
*/
@property(nonatomic, strong)UIImageView *imageView;
/**
移动视图的文字
*/
@property(nonatomic, strong)UILabel *labelView;
@endmoveView.m文件代码:
//
// moveView.m
// ZQMoveViewClick
//
// Created by 赵前 on 16/6/1.
// Copyright © 2016年 赵前. All rights reserved.
//
#import "moveView.h"
@implementation moveView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor grayColor];
[self AddSubViews];
}
return self;
}
#pragma mark *** Private Method ***
- (void)AddSubViews{
[self addSubview:self.imageView];
[self addSubview:self.labelView];
}
#pragma mark *** Lazy Loading ***
- (UIImageView *)imageView{
if (!_imageView) {
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width*0.6, self.bounds.size.height*0.6)];
_imageView.center = CGPointMake(self.bounds.size.width/2, 5 + _imageView.bounds.size.height/2);
_imageView.backgroundColor = [UIColor greenColor];
}
return _imageView;
}
- (UILabel *)labelView{
if (!_labelView) {
_labelView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,self.bounds.size.width*0.3, self.bounds.size.height*0.3)];
_labelView.center = CGPointMake(self.bounds.size.width/2, CGRectGetMaxY(_imageView.frame) + 5 + _labelView.bounds.size.height/2);
_labelView.text = @"test";
_labelView.backgroundColor = [UIColor orangeColor];
}
return _labelView;
}
@end
视图封装好了之后,就可以在MoveViewController操作了。
MoveViewController.h文件代码如下:
//
// MoveViewController.h
// ZQMoveViewClick
//
// Created by 赵前 on 16/6/1.
// Copyright © 2016年 赵前. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MoveViewController : UIViewController
/**
moveViewCount 创建的要移动的视图数量
*/
-(void)CreateMoveView;
@endMoveViewController.m代码如下
//
// MoveViewController.m
// ZQMoveViewClick
//
// Created by 赵前 on 16/6/1.
// Copyright © 2016年 赵前. All rights reserved.
//
#import "MoveViewController.h"
#import "moveView.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define Tag 100
@interface MoveViewController ()
{
NSTimer *_timer;/**< 设置气泡滚动开始时间*/
NSArray *_dataSource;
}
@end
@implementation MoveViewController
- (void)initializeDataSource{
_dataSource = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
}
-(void)CreateMoveView{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondsToGesture:)];
_timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(respondsToTimer:) userInfo:nil repeats:YES];
int wid = WIDTH - 100;
int hei = HEIGHT - 124;
for (int i = 0; i < _dataSource.count; i++) {
CGFloat w = (arc4random() % wid + 0);
CGFloat h = (arc4random() % hei + 0);
moveView *bubble = [[moveView alloc]initWithFrame:CGRectMake(0, 0, 100, 80)];
bubble.userInteractionEnabled = YES;
bubble.center = CGPointMake(w, h);
bubble.tag = Tag + i;
bubble.layer.anchorPoint = CGPointMake(0, 0);
[bubble.layer setBackgroundColor:[UIColor clearColor].CGColor];
[self.view.layer addSublayer:bubble.layer];
[self.view addGestureRecognizer:tap];
[self.view addSubview:bubble];
}
}
#pragma mark *** Events ***
-(void)respondsToGesture:(UITapGestureRecognizer *)gesture {
// ShowInfoViewController *showInfoVc = [ShowInfoViewController new];
CGPoint touchPoint = [gesture locationInView:self.view];
for (int i = (int)_dataSource.count; i >=0; i--) {
UIView *temView = [self.view viewWithTag:(Tag + i)];
if ([temView.layer.presentationLayer hitTest:touchPoint]) {
temView.backgroundColor = [UIColor blueColor];
// [self.navigationController pushViewController:showInfoVc animated:YES];
return;
}
}
}
-(void)viewWillAppear:(BOOL)animated
{
[_timer setFireDate:[NSDate distantPast]];
}
-(void)viewDidDisappear:(BOOL)animated
{
[_timer setFireDate:[NSDate distantFuture]];
}
-(void)respondsToTimer:(NSTimer *)timer
{
int wid = WIDTH - 100;
int hei = HEIGHT - 124;
for (int i = 0; i < (int)_dataSource.count; i++) {
CGFloat w = (arc4random() % wid + 0);
CGFloat h = (arc4random() % hei + 0);
CGPoint poi = CGPointMake(w, h);
CAKeyframeAnimation *moveLayerAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
UIView *temView = [self.view viewWithTag:(Tag + i)];
moveLayerAnimation.values = @[[NSValue valueWithCGPoint:temView.center],
[NSValue valueWithCGPoint:poi]];
temView.center = poi;
moveLayerAnimation.duration = 20;
moveLayerAnimation.autoreverses = YES;
moveLayerAnimation.repeatCount = INFINITY;
moveLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[temView.layer addAnimation:moveLayerAnimation forKey:@"move"];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeDataSource];
self.view.backgroundColor = [UIColor whiteColor];
[self CreateMoveView];
}
@end
到此,就可以看见点击的效果了。
注意:这个代码只是简单的做出了效果,可以进行更好的封装实现,宏定义的Tag值是为了判断当前点击的是哪个View,以便进行取值,或者跳转到下一个界面的时候知道是点击的哪个view。
另外说明的一点是可以根据视图的层级点击(比如某个视图被一个视图遮住了,点击的时候只有在最上面的视图才会发生颜色的变化),如果点击之后要进一步操作,可以在下面函数中写
-(void)respondsToGesture:(UITapGestureRecognizer *)gesture {
// ShowInfoViewController *showInfoVc = [ShowInfoViewController new];
CGPoint touchPoint = [gesture locationInView:self.view];
for (int i = (int)_dataSource.count; i >=0; i--) {
UIView *temView = [self.view viewWithTag:(Tag + i)];
if ([temView.layer.presentationLayer hitTest:touchPoint]) {
temView.backgroundColor = [UIColor blueColor];
// [self.navigationController pushViewController:showInfoVc animated:YES];
//可以在这里写你想要做的事情
return;
}
}
}
效果图如下:
新建工程,先删除文件中的ViewController.h和ViewController.m文件,新建MoveViewController继承于UIViewController
将MoveViewController作为window的根视图控制器,代码如下
在AppDelegate.m文件导入头文件
#import "MoveViewController.h"
接着在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {}
函数中添加如下代码,并且在Info.plist文件中将Main storyboard file base name关键字段的Main删除
MoveViewController *moveViewVc = [[MoveViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:moveViewVc];
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.rootViewController = nav;
[_window makeKeyAndVisible];
现在根视图控制变成MoveViewController,然后就可以在MoveViewController中进行操作了
在这之前我们需要先封装一个移动视图
新建文件继承于UIView
moveView.h文件代码如下:
//
// moveView.h
// ZQMoveViewClick
//
// Created by 赵前 on 16/6/1.
// Copyright © 2016年 赵前. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface moveView : UIView
/**
移动视图的头部视图
*/
@property(nonatomic, strong)UIImageView *imageView;
/**
移动视图的文字
*/
@property(nonatomic, strong)UILabel *labelView;
@endmoveView.m文件代码:
//
// moveView.m
// ZQMoveViewClick
//
// Created by 赵前 on 16/6/1.
// Copyright © 2016年 赵前. All rights reserved.
//
#import "moveView.h"
@implementation moveView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor grayColor];
[self AddSubViews];
}
return self;
}
#pragma mark *** Private Method ***
- (void)AddSubViews{
[self addSubview:self.imageView];
[self addSubview:self.labelView];
}
#pragma mark *** Lazy Loading ***
- (UIImageView *)imageView{
if (!_imageView) {
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width*0.6, self.bounds.size.height*0.6)];
_imageView.center = CGPointMake(self.bounds.size.width/2, 5 + _imageView.bounds.size.height/2);
_imageView.backgroundColor = [UIColor greenColor];
}
return _imageView;
}
- (UILabel *)labelView{
if (!_labelView) {
_labelView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,self.bounds.size.width*0.3, self.bounds.size.height*0.3)];
_labelView.center = CGPointMake(self.bounds.size.width/2, CGRectGetMaxY(_imageView.frame) + 5 + _labelView.bounds.size.height/2);
_labelView.text = @"test";
_labelView.backgroundColor = [UIColor orangeColor];
}
return _labelView;
}
@end
视图封装好了之后,就可以在MoveViewController操作了。
MoveViewController.h文件代码如下:
//
// MoveViewController.h
// ZQMoveViewClick
//
// Created by 赵前 on 16/6/1.
// Copyright © 2016年 赵前. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MoveViewController : UIViewController
/**
moveViewCount 创建的要移动的视图数量
*/
-(void)CreateMoveView;
@endMoveViewController.m代码如下
//
// MoveViewController.m
// ZQMoveViewClick
//
// Created by 赵前 on 16/6/1.
// Copyright © 2016年 赵前. All rights reserved.
//
#import "MoveViewController.h"
#import "moveView.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define Tag 100
@interface MoveViewController ()
{
NSTimer *_timer;/**< 设置气泡滚动开始时间*/
NSArray *_dataSource;
}
@end
@implementation MoveViewController
- (void)initializeDataSource{
_dataSource = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
}
-(void)CreateMoveView{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondsToGesture:)];
_timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(respondsToTimer:) userInfo:nil repeats:YES];
int wid = WIDTH - 100;
int hei = HEIGHT - 124;
for (int i = 0; i < _dataSource.count; i++) {
CGFloat w = (arc4random() % wid + 0);
CGFloat h = (arc4random() % hei + 0);
moveView *bubble = [[moveView alloc]initWithFrame:CGRectMake(0, 0, 100, 80)];
bubble.userInteractionEnabled = YES;
bubble.center = CGPointMake(w, h);
bubble.tag = Tag + i;
bubble.layer.anchorPoint = CGPointMake(0, 0);
[bubble.layer setBackgroundColor:[UIColor clearColor].CGColor];
[self.view.layer addSublayer:bubble.layer];
[self.view addGestureRecognizer:tap];
[self.view addSubview:bubble];
}
}
#pragma mark *** Events ***
-(void)respondsToGesture:(UITapGestureRecognizer *)gesture {
// ShowInfoViewController *showInfoVc = [ShowInfoViewController new];
CGPoint touchPoint = [gesture locationInView:self.view];
for (int i = (int)_dataSource.count; i >=0; i--) {
UIView *temView = [self.view viewWithTag:(Tag + i)];
if ([temView.layer.presentationLayer hitTest:touchPoint]) {
temView.backgroundColor = [UIColor blueColor];
// [self.navigationController pushViewController:showInfoVc animated:YES];
return;
}
}
}
-(void)viewWillAppear:(BOOL)animated
{
[_timer setFireDate:[NSDate distantPast]];
}
-(void)viewDidDisappear:(BOOL)animated
{
[_timer setFireDate:[NSDate distantFuture]];
}
-(void)respondsToTimer:(NSTimer *)timer
{
int wid = WIDTH - 100;
int hei = HEIGHT - 124;
for (int i = 0; i < (int)_dataSource.count; i++) {
CGFloat w = (arc4random() % wid + 0);
CGFloat h = (arc4random() % hei + 0);
CGPoint poi = CGPointMake(w, h);
CAKeyframeAnimation *moveLayerAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
UIView *temView = [self.view viewWithTag:(Tag + i)];
moveLayerAnimation.values = @[[NSValue valueWithCGPoint:temView.center],
[NSValue valueWithCGPoint:poi]];
temView.center = poi;
moveLayerAnimation.duration = 20;
moveLayerAnimation.autoreverses = YES;
moveLayerAnimation.repeatCount = INFINITY;
moveLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[temView.layer addAnimation:moveLayerAnimation forKey:@"move"];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeDataSource];
self.view.backgroundColor = [UIColor whiteColor];
[self CreateMoveView];
}
@end
到此,就可以看见点击的效果了。
注意:这个代码只是简单的做出了效果,可以进行更好的封装实现,宏定义的Tag值是为了判断当前点击的是哪个View,以便进行取值,或者跳转到下一个界面的时候知道是点击的哪个view。
另外说明的一点是可以根据视图的层级点击(比如某个视图被一个视图遮住了,点击的时候只有在最上面的视图才会发生颜色的变化),如果点击之后要进一步操作,可以在下面函数中写
-(void)respondsToGesture:(UITapGestureRecognizer *)gesture {
// ShowInfoViewController *showInfoVc = [ShowInfoViewController new];
CGPoint touchPoint = [gesture locationInView:self.view];
for (int i = (int)_dataSource.count; i >=0; i--) {
UIView *temView = [self.view viewWithTag:(Tag + i)];
if ([temView.layer.presentationLayer hitTest:touchPoint]) {
temView.backgroundColor = [UIColor blueColor];
// [self.navigationController pushViewController:showInfoVc animated:YES];
//可以在这里写你想要做的事情
return;
}
}
}
效果图如下:
相关文章推荐
- iOS热文欣赏
- 【代码笔记】iOS-竖状图
- 【代码笔记】iOS-竖状图
- iOS代码加密常用加密方式
- IOS设置View的背景图片
- ps字体与ios字体转换
- iOS页面传值-wang
- ios常见的页面传值方式
- iOS内存分配大小
- Xcode Unknown type name 'NSString'
- ios获取设备唯一标识
- ios图文混排
- iOS毛玻璃效果
- ios aes 加密 与 nodejs aes 不一样 解决方法
- IOS博客项目搭建-19-项目重构-封装业务工具类
- iOS沙盒机制
- ios runtime基础知识
- iOS监听系统键盘的弹起和回收
- 【转】【iOS】导航栏那些事儿
- 【转】iOS中设置导航栏标题的字体颜色和大小