iOS iPhone中的可拖动按钮,以便在触摸结束后将其自身重新放置在其原始位置。 [英] Drag-able button in iOS iPhone such that it repositions itself in its orignal position after touch ended.
问题描述
我正在制作一个应用程序,其中有用于不同颜色的不同按钮,如果我在特定视图上拖动该颜色按钮,则其颜色必须更改为该按钮的颜色,并且该颜色按钮应重新放置在其原始位置。我尝试了各种方法,例如开始触摸和结束触摸,但这似乎无法解决我的问题。我还尝试了可在各种uicontrolstate上触发的自定义方法,但它也无法正常工作。请帮我解决这个问题。
I am making an app where there are different buttons for different colors, if i drag this color button on a specific view its color must change to this button's color and the color button should reposition in its orignal location. I tried various methods like touches began and touches ended but it doesn't seem to solve my issue. I also tried customized methods which fire on various uicontrolstate but it didnt work as well. plz help me out with this.
推荐答案
我认为您使用touchesBegan,touchesMoved和touchesEnded可以解决问题。
I think you use touchesBegan,touchesMoved and touchesEnded can solve the problem.
touchesBe开始标记orignPosition。
touchesBegan is to mark the orignPosition.
CGPoint orignPosition;
CGColor buttonColor;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
UIView *touchView = [touch view];
if ([touchView isKindOfClass:[UIButton class]])
{
orignPosition = (UIButton *)touchView.center;
buttonColor = (UIButton *)touchView.backgroundColor;
}
}
touchesMoved是用手指移动按钮
touchesMoved is to let the button move with your finger
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
UIView *touchView = [touch view];
CGPoint movedPoint = [touch locationInView:yourMainView];
CGPoint deltaVector = CGPointMake(movedPoint.x - lastTouchPoint.x, movedPoint.y - lastTouchPoint.y);
lastTouchPoint = movedPoint;
if ([touchView isKindOfClass:[UIButton class]])
{
touchView.center = CGPointMake(touchView.center.x + deltaVector.x, touchView.center.y + deltaVector.y);
}
}
touchesEnded是判断是否更改您的特殊视图颜色
touchesEnded is to judge whether change your special view`s color
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
UIView *touchView = [touch view];
CGPoint movedPoint = [touch locationInView:scrollView];
if ([touchView isKindOfClass:[UIButton class]])
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
(UIButton *)touchView.center = orignPosition;
[UIView commitAnimations];
}
if(CGRectContainsPoint(specialView.frame, [touch locationInView:yourMainView]))
{
[yourMainView setBackgroundColor:buttonColor];
}
}
这篇关于iOS iPhone中的可拖动按钮,以便在触摸结束后将其自身重新放置在其原始位置。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!