手势(3)view随手势移动

#import "QYCustomView.h"

/*
#define  HOR_SWIPE_MIN 20 //水平上,当低于这个值的时候, 不认为他是一个横扫的手势
#define  VAR_SWIPE_MAX 40 //在垂直上,设置这误差范围,如果大于这个值的话, 横扫无效
 */

@implementation QYCustomView

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor purpleColor];
        _moveView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 30, 30)];
        _moveView.backgroundColor = [UIColor redColor];
        [self addSubview:_moveView];
    }
    return self;
}

//当我们的事件开始的时候调, 对于touch来说, 实际上当手指头放到屏幕上的时候,这个方法会被调用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _moveView = [[UIView alloc ]initWithFrame:CGRectMake(0, 0, 30, 30)];
    _moveView.backgroundColor = [UIColor redColor];
    [self addSubview:_moveView];
}



////当我们手指点击屏幕,并且没有抬起而滑动的时候, 这个方法被调用, 由此可知这个方法是连续被调用的。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *tch = [touches anyObject];
    CGPoint currentPoint = [tch locationInView:self];
    
//    CGRect frame = self.moveView.frame;
    self.moveView.center = currentPoint;
    
    NSLog(@"%s",__func__);
}



//当我们的事件对束的时候调,对于touch来说, 实际上是当手指头离开屏幕的时候,这个方法会被调用
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}

/*
//
////当我们的事件被取消的时候, 比如说:手指点击在屏幕上, 突然来电话, 这个时候, 电话的优先级很高,
////所有的事件都应该被取消。 这个方法会被调用
//- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    NSLog(@"%s",__func__);
//    self.startPoint = CGPointZero;
//}
*/

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    self.backgroundColor = [UIColor orangeColor];
}
 */
@end

posted @ 2014-07-16 14:07  平淡的我  阅读(348)  评论(0编辑  收藏  举报