1

我正在开发一个示例应用程序,在其中我有一个在屏幕上移动 UITextFields、UILabels 等的情况。我找不到任何资源来实现这个。请给我一个相同的解决方案。

我想为 UITextField实现这样的

提前感谢大家。 所有stackoverflow用户的圣诞快乐和高级新年祝福

4

2 回答 2

2

由于您已经有一个关于拖动的优秀教程,我假设您的问题是当您尝试拖动 UITextField 而不是 UIView 时出现的键盘。解决方案应该非常简单:myTextField.userInteractionEnabled = NO;- 应该禁用用户交互并使其“只读”。也许有一个编辑模式,其中所有文本字段都设置了这个标志。如果它引起问题,则将 textField 添加到 UIView,然后将 userInteractionEnabled 设置为 false。然后你可以拖动 UIView,它会拖动文本字段。

希望对您有所帮助,也祝您节日快乐!

于 2010-12-25T05:55:10.520 回答
2

我遵循了迈克尔的建议并得到了解决方案。我在下面给出了代码片段,这对那些需要实现相同功能的人很有用。

脚步:

选择基于窗口的应用程序,然后创建一个 UIViewController 子类并将其添加到 appdelegate 文件中的窗口中。

在您创建的视图控制器类的 XIB 中,添加 UIViews 并将文本字段等控件添加到您创建的 UIViews。我们将只移动这些视图,因此在视图控制器子类的 .h 文件中添加 IBOutlets并将它们相应地映射到 IB。

示例代码

应用代理.h

#import <UIKit/UIKit.h>
#import "MyView.h"

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;
MyView *viewController;
 }

@property (nonatomic, retain) IBOutlet UIWindow *window;
@end

appdelegate.m

#import "MyAppDelegate.h"


@implementation MyAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    // Override point for customization after application launch.
    viewController=[[MyView alloc]init];
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

}

- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


@end

视图控制器.h

#import <UIKit/UIKit.h>


@interface MyView : UIViewController {

    IBOutlet UIView *textFieldView;
    IBOutlet UIView *labelView;

}

@end

视图控制器.m

#import "MyView.h"


@implementation MyView


- (void)viewDidLoad {

    [self.view addSubview:textFieldView];
    [self.view addSubview:labelView];
    [super viewDidLoad];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if ([touch view] == textFieldView) {
        // move the image view
        textFieldView.center = touchLocation;
    }
    if ([touch view] == labelView) {
        // move the image view
        labelView.center = touchLocation;
    }

}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

谢谢大家。有一个侄女的时间。

于 2010-12-25T07:41:01.873 回答