侧滑

下面是来客网 jb51.cc 通过网络收集整理的代码片段。

来客网小编现在分享给大家,也给大家做个参考。

//
//  ViewController.m
//  CeHua
//
//  Created by mac on 15-4-14.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "ViewController.h"
#import "LeftViewController.h"
#import "RightViewController.h"

//取屏幕寬高
#define KscreenWidth [UIScreen mainScreen].bounds.size.width
#define KscreenHeigh [UIScreen mainScreen].bounds.size.height

@interface ViewController (){

    CGPoint _point0;//初始point
    
    LeftViewController *_left;
    RightViewController *_right;

}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view,typically from a nib.
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    //初始化左右視圖
    _left = [[LeftViewController alloc] init];
    _left.view.frame = CGRectMake(-200,KscreenWidth,KscreenHeigh);
    _left.view.alpha = 0;
    
    _right = [[RightViewController alloc] init];
    _right.view.frame = CGRectMake((KscreenWidth - (KscreenWidth/4)),KscreenHeigh);
    _right.view.alpha = 0;
    
    /*
     
     第二种方式
     
     在这里不设置frame值  把左右视图当做子视图加到主视图上 当触摸结束时改变左右视图的frame值
     
     第三种方式
     
     把左右控制器当子控制器加到主控制器上 触摸结束时改变frame值并把视图插入主界面
     */
    
}


//觸摸開始時的事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    //取觸摸點座標
    UITouch *touch = [touches anyObject];
    _point0 = [touch locationInView:self.view];
    UIImageView *iamgeView = [[UIImageView alloc] initWithFrame:CGRectMake(_point0.x,_point0.y,20,20)];
    iamgeView.image = [UIImage imageNamed:@"0.jpg"];
    [self.view addSubview:iamgeView];
    
    [UIView animateWithDuration:.75
                     animations:^{
                         iamgeView.alpha = 0;
                     } completion:^(BOOL finished) {
                         [iamgeView removeFromSuperview];
                     }];
    
}

//手指移動時的時間
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    UIImageView *iamgeView = [[UIImageView alloc] initWithFrame:CGRectMake(point.x,point.y,20)];
    iamgeView.image = [UIImage imageNamed:@"0.jpg"];
    [self.view addSubview:iamgeView];

    
    [UIView animateWithDuration:.5
                     animations:^{
                         iamgeView.alpha = 0;
                     } completion:^(BOOL finished) {
                         [iamgeView removeFromSuperview];
                     }];

    
}


//觸摸結束時的事件
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{


    //判斷左右視圖出現的時機
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    
    if (_left.view == nil) {
        
        _left = [[LeftViewController alloc] init];
        _left.view.frame = CGRectMake(-200,KscreenHeigh);
        _left.view.alpha = 0;

        
    }
    if (_right.view == nil) {
        
        
        _right = [[RightViewController alloc] init];
        _right.view.frame = CGRectMake((KscreenWidth - (KscreenWidth / 4)),KscreenHeigh);
        _right.view.alpha = 0;

    }
    
    if (200 < (point.x - _point0.x)) {
        
        
        [UIView animateWithDuration:.75
                         animations:^{
                             
                             if (_right.view.alpha == 0) {
                                 
                                 _left.view.alpha = 1;
                                 [self.view addSubview:_left.view];
                             }
                             
                             _right.view.alpha = 0;
                             [_right.view removeFromSuperview];
                             
                         } completion:^(BOOL finished) {
                             
                         }];

        
    }else if (200 < (_point0.x - point.x)){
    
    
        [UIView animateWithDuration:.75
                         animations:^{
                             
                             if (_left.view.alpha == 0) {
                                 
                                 _right.view.alpha = 1;
                                 [self.view addSubview:_right.view];
                                 
                             }
                             
                             _left.view.alpha = 0;
                             [_left.view removeFromSuperview];
                             
                             
                         } completion:^(BOOL finished) {
                             
                         }];
    
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

以上是来客网(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。

以上是来客网为你收集整理的侧滑全部内容,希望文章能够帮你解决侧滑所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。