手勢(3)view隨手勢移動

來源:互聯網
上載者:User

標籤:color   art   for   io   re   c   

#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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.