无法使用 UIPanGestureRecognizer 移动 UIImageView

发布于 2024-10-09 01:32:48 字数 1974 浏览 0 评论 0原文

我正在尝试创建一个程序,用户可以使用 UIPanGestureRecognizer 在屏幕上拖动 UIImageView。我尝试了几种不同的方法,但无法弄清楚。我不确定是否需要创建发送者/请求者。我的理解是UIImageView需要放置在可以处理手势的UIView中。在我的程序中,我有一个视图控制器 - AdvancedViewController1。我创建了一个名为 AdvancedView1 的 UIView。在界面生成器中,我已将 AdvancedView1 插入 AdvancedViewController 1。以下是控制器和视图的 .h.m 文件。我只包含了相关代码。请让我知道我是否接近,以及如何修复我的代码。预先感谢您的帮助。

AdvancedViewController1.h
#import <UIKit/UIKit.h>
#import "AdvancedView1.h"
@interface AdvancedViewController1 : UIViewController {
UIWindow *window;
AdvancedView1 *advancedView1;
UIImageView * option1; 
}
@property (retain) IBOutlet AdvancedView1 *advancedView1;
@property (retain) IBOutlet UIImageView *option1;
@end

在 IB 中,我已将 IBOutlet 链接到 AdvancedView1 到 UIView,将 option1 链接到我希望能够移动的 UIImageView。

AdvancedViewController.m

#import "AdvancedViewController1.h"
#import "AdvancedView1.h"
@implementation AdvancedViewController1
@synthesize advancedView1;
@synthesize option1

- (void)viewDidLoad { 
UIGestureRecognizer *pangr = [[UIPanGestureRecognizer alloc]       initWithTarget:self.advancedView1 action:@selector(pan:)];
pangr.delegate = self;
[self.advancedView1 addGestureRecognizer:pangr];
[pangr release];      
[super viewDidLoad];
}

AdvancedView1.h

#import <UIKit/UIKit.h>
#import "AdvancedViewController1.h"
@class AdvancedView1;
@interface AdvancedView1 : UIView{
CGPoint* origin;
}
@property (nonatomic)  CGPoint* origin;
@end

AdvancedView1.m
#import "AdvancedView1.h"
#import "AdvancedViewController1.h"
@implementation AdvancedView1;
@synthesize origin;

- (void)pan:(UIPanGestureRecognizer *) gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {

CGPoint translation = [gesture translationInView:self];
gesture.origin = CGPointMake(gesture.origin.x+translation.x,        gesture.origin.y+translation.y);
[gesture setTranslation:CGPointZero inView:self];
}

I am trying to create a program where a user can drag a UIImageView around the screen by using UIPanGestureRecognizer. I have tried a few different ways but cannot figure it out. I am not sure if I need to create a sender/requestor. My understanding is that the UIImageView needs to be placed in a UIView that can handle gestures. In my program I have a view controller - AdvancedViewController1. I have created a UIView named AdvancedView1. In interface Builder I have inserted AdvancedView1 into the AdvancedViewController 1. The following are my .h and .m files for the controller and the view. I have only included the relevant code. Please let me know if I am close, and how to fix my code. Thanks in advance for your help.

AdvancedViewController1.h
#import <UIKit/UIKit.h>
#import "AdvancedView1.h"
@interface AdvancedViewController1 : UIViewController {
UIWindow *window;
AdvancedView1 *advancedView1;
UIImageView * option1; 
}
@property (retain) IBOutlet AdvancedView1 *advancedView1;
@property (retain) IBOutlet UIImageView *option1;
@end

In IB I have linked an IBOutlet to AdvancedView1 to the UIView and option1 to the UIImageView that I want to be able to move.

AdvancedViewController.m

#import "AdvancedViewController1.h"
#import "AdvancedView1.h"
@implementation AdvancedViewController1
@synthesize advancedView1;
@synthesize option1

- (void)viewDidLoad { 
UIGestureRecognizer *pangr = [[UIPanGestureRecognizer alloc]       initWithTarget:self.advancedView1 action:@selector(pan:)];
pangr.delegate = self;
[self.advancedView1 addGestureRecognizer:pangr];
[pangr release];      
[super viewDidLoad];
}

AdvancedView1.h

#import <UIKit/UIKit.h>
#import "AdvancedViewController1.h"
@class AdvancedView1;
@interface AdvancedView1 : UIView{
CGPoint* origin;
}
@property (nonatomic)  CGPoint* origin;
@end

AdvancedView1.m
#import "AdvancedView1.h"
#import "AdvancedViewController1.h"
@implementation AdvancedView1;
@synthesize origin;

- (void)pan:(UIPanGestureRecognizer *) gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {

CGPoint translation = [gesture translationInView:self];
gesture.origin = CGPointMake(gesture.origin.x+translation.x,        gesture.origin.y+translation.y);
[gesture setTranslation:CGPointZero inView:self];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-10-16 01:32:48

您必须在 pan: 选择器中实际设置 UIView 的位置。由于您是在 AdvancedView 类中而不是在其外部执行此操作,因此您必须获取视图的父视图([self superview]),因为更新位置是相对于父(包含)视图的。试试这个:

- (void)pan:(UIPanGestureRecognizer *)gesture
{
  if ((gesture.state == UIGestureRecognizerStateChanged) ||
      (gesture.state == UIGestureRecognizerStateEnded)) {

    CGPoint location = [gesture locationInView:[self superview]];

    [self setCenter:location];
  }
}

请记住,您需要确保您的子类视图支持用户交互,否则手势识别器将无法工作。只需在初始化视图时使用 [self setUserInteractionEnabled:YES] 将其打开即可。

You have to actually set the position of UIView in your pan: selector. And since you're doing it within your AdvancedView class rather than outside of it, you have to get the view's parent view ([self superview]) since updating the position is relative to the parent (containing) view. Try this:

- (void)pan:(UIPanGestureRecognizer *)gesture
{
  if ((gesture.state == UIGestureRecognizerStateChanged) ||
      (gesture.state == UIGestureRecognizerStateEnded)) {

    CGPoint location = [gesture locationInView:[self superview]];

    [self setCenter:location];
  }
}

Keep in mind that you need to make sure your subclassed view enables user interaction or gesture recognizers won't work. Just turn it on with [self setUserInteractionEnabled:YES] when you init the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文