iphone - UIControlEventTouchDragInside 问题

标签 iphone ios objective-c

在 Viewdidload 中,CandidateView 是自定义 View Controller

for (int i = 0; i < 9 ; i++) {
    for (int j = 0; j < 3; j++) {
        CandidateView *candView = [[CandidateView alloc]init];
        candView.view.frame = CGRectMake(15 + i*186 ,17 + j*145, 158,130);
        [candView.btnCandidate addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
        [mainScroll addSubview:candView.view];
    }
}

这是方法

-(void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{

    UITouch *touch = [[event touchesForView:button] anyObject];
    // get delta
    CGPoint previousLocation = [touch previousLocationInView:button];
    CGPoint location = [touch locationInView:button];
    CGFloat delta_x = location.x - previousLocation.x;
    CGFloat delta_y = location.y - previousLocation.y;

    // move button
    button.center = CGPointMake(button.center.x + delta_x,button.center.y + delta_y);
}

通过这段代码,我可以在 CandidateView 中移动一个按钮。现在我想拖动整个 CandidateView,或者我应该制作 UIView 而不是 UIViewcontroller。 谁能帮我?? 提前致谢..:)

最佳答案

制作一个 UIView 并将所有内容放入其中,UIView 应该填满“UIViewController”的屏幕,拖动方法也应该可以正常工作 :) 祝你好运! (*您可能想要研究的另一个解决方案是制作一个 UIScrollView,其中包含嵌套在其中的所有对象)

关于iphone - UIControlEventTouchDragInside 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14874294/

相关文章:

ios - AVAudioRecorder 设置没有对输出文件生效

iphone - 在所有内容(甚至键盘)之上显示 uiview

ios - iOS 6 上的低功耗蓝牙; CBPeripheralManagerDelegate

iOS:应用程序打开时显示通知消息

ios - 使用 NSDocumentDirectory 从 localPath 中删除每个单独的图像

iphone - 如何从 iPhone 应用程序调用 Web 服务

iphone - 在第三方应用程序中显示存折中的现有通行证?

ios - Xcode9 iOS 导出存档以正确的格式失败

ios - Unity 3.5 到 Unity 4.5.4

ios - OS X Cocoa 颜色组合框