1. 程式人生 > >iOS移動View點選事件

iOS移動View點選事件

在做開發的時候發現移動中的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;

@end
moveView.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;

@end
MoveViewController.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;
        }
    }
}

效果圖如下: