Cocoa touch 在我的iPad应用程序中拖放
我从另一位先生那里改编了这段代码。到目前为止,它工作得很好,但在我的应用程序中,我计划有很多可以移动的图像视图/按钮。实现这一点的最佳方式是什么?也许将对象放入数组并使用枚举器?如果其他任何人也做过类似的事情,那么在正确的方向上轻推一下将是非常感激的! 顺便说一下,它们在视图中都有不同的坐标Cocoa touch 在我的iPad应用程序中拖放,cocoa-touch,drag-and-drop,uikit,uiimageview,Cocoa Touch,Drag And Drop,Uikit,Uiimageview,我从另一位先生那里改编了这段代码。到目前为止,它工作得很好,但在我的应用程序中,我计划有很多可以移动的图像视图/按钮。实现这一点的最佳方式是什么?也许将对象放入数组并使用枚举器?如果其他任何人也做过类似的事情,那么在正确的方向上轻推一下将是非常感激的! 顺便说一下,它们在视图中都有不同的坐标 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIPanGes
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragGesture:)];
[dragImage addGestureRecognizer:panGesture];
[dragImage setUserInteractionEnabled:YES];
}
#pragma mark -
#pragma mark UIPanGestureRecognizer selector
- (void) dragGesture:(UIPanGestureRecognizer *) panGesture{
CGPoint translation = [panGesture translationInView:self.view];
switch (panGesture.state) {
case UIGestureRecognizerStateBegan:{
originalCentre = dragImage.center;
}
break;
case UIGestureRecognizerStateChanged:{
dragImage.center = CGPointMake(dragImage.center.x + translation.x,
dragImage.center.y + translation.y);
}
break;
case UIGestureRecognizerStateEnded:{
if (((dragImage.center.x >= 280) && (dragImage.center.y >= 280) &&
(dragImage.center.x <= 450) && (dragImage.center.y <= 450))) {
dragImage.center = CGPointMake(300, 300);
[dragImage setUserInteractionEnabled:NO];
break;
}
[UIView animateWithDuration:0.5
animations:^{
dragImage.center = originalCentre;
}
completion:^(BOOL finished){
}];
}
break;
default:
break;
}
[panGesture setTranslation:CGPointZero inView:self.view];
}
-(IBAction)clear{
[UIImageView animateWithDuration:0.5 animations:^{
dragImage.center = CGPointMake(100, 200);
}];
[dragImage setUserInteractionEnabled:YES];
}
-(void)viewDidLoad
{
[超级视图下载];
//加载视图后执行任何其他设置。
UIPangestureRecognitzer*panGesture=[[UIPangestureRecognitzer alloc]initWithTarget:self action:@selector(dragGesture:)];
[dragImage addgesture识别器:panGesture];
[dragImage setUserInteractionEnabled:是];
}
#布拉格标记-
#pragma标记UIPANGESTURE识别器选择器
-(无效)dragGesture:(UIPangesture识别器*)panGesture{
CGPoint translation=[panGesture translationView:self.view];
开关(panGesture.state){
案例UIgestureRecognitzerStateStart:{
原始中心=dragImage.center;
}
打破
案例UIgestureRecognitzerStateChanged:{
dragImage.center=CGPointMake(dragImage.center.x+translation.x,
dragImage.center.y+translation.y);
}
打破
案例UIgestureRecognitizerStateEnded:{
如果(((dragImage.center.x>=280)和&(dragImage.center.y>=280)&&
(dragImage.center.x您可以使用recognizer.view
来确定正在移动的视图。不要总是更改drawImage
,只需使用recognizer.view
。当识别器开始时,确保取消它(通过将enabled
设置为NO
然后设置为YES
)如果它是您不想拖放的子视图
根据要移动的视图,您可能需要更改其中一些幻数。谢谢@Ash。您的意思是使用识别器更改每个绘图。视图?事实上,对不起,我撒谎了。在方法中,您正在移动(更改的帧)drawImage
。您需要将其更改为移动用户手指所在的任何视图。或者,您可以将多个手势识别器连接到要移动的每个视图。