平移視圖效果

//
//  CLTranslationView.m
//  LessonUIEvent
//
//  Created by lanouhn on 14-8-25.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import "CLTranslationView.h"

@interface CLTranslationView ()
{
    CGPoint _previousPoint;//存儲移動之前的點的位置
}
@end


@implementation CLTranslationView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //用來設置當前視圖是否支持多點觸摸. iOS雖然支持多次觸摸, 但是默認的是單點觸摸
        self.multipleTouchEnabled = YES;
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%lu", (unsigned long)touches.count);
    UITouch *touch = [touches anyObject];
    //獲取手指觸摸在視圖上的位置
    _previousPoint = [touch locationInView:self];
    //膨脹檢測 有大到小
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //獲取手指對象
    UITouch *touch = [touches anyObject];
    //獲取移動之後手指在視圖上的位置
    CGPoint currentPoint = [touch locationInView:self];
    //獲取移動之前和移動之後的座標變化量
    CGFloat dx = currentPoint.x - _previousPoint.x;
    CGFloat dy = currentPoint.y - _previousPoint.y;
    self.center = CGPointMake(self.center.x + dx, self.center.y + dy);
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章