今天把快速点餐的那个“球”移动起来了,可以通过UIPanGestureRecognizer进行操作。
- 是在窗口的UIView下面建立一个subView,做一个圆形的UIView,涂色为红色。
circleView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; CALayer *layer = [circleView layer]; layer.cornerRadius = 50; [layer setMasksToBounds:YES];circleView.userInteractionEnabled = YES;circleView.backgroundColor = [UIColor redColor]; [self.view addSubview:_circleView];
- 添加UIPanGestureRecognizer手势
UIPanGestureRecognizer *ges = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGes:)]; [_circleView addGestureRecognizer:ges];
- 实现移动,在handlePanGes函数下面.
- (void)handlePanGes:(UIPanGestureRecognizer *)ges {
CGPoint pt = [ges translationInView:self.view]; [circleView setCenter:CGPointMake(circleView.center.x + pt.x, _circleView.center.y + pt.y)];[ges setTranslation:CGPointMake(0, 0) inView:self.view]; }
UIPanGestureRecognizer是实现一个偏移量,最后一行代码是把偏移量重置为0,不然的话偏移量会累积。代码在github上面。