iphone - 无法使用 UIPanGestureRecognizer 移动 UIImageView

标签 iphone ipad uigesturerecognizer

我正在尝试创建一个程序,用户可以使用 UIPanGestureRecognizer 在屏幕上拖动 UIImageView。我尝试了几种不同的方法但无法弄清楚。我不确定是否需要创建发送者/请求者。我的理解是UIImageView需要放置在可以处理手势的UIView中。在我的程序中,我有一个 View Controller - AdvancedViewController1。我创建了一个名为 AdvancedView1 的 UIView。在界面生成器中,我已将 AdvancedView1 插入 AdvancedViewController 1。以下是 Controller 和 View 的 .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];
}

最佳答案

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

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

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

    [self setCenter:location];
  }
}

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

关于iphone - 无法使用 UIPanGestureRecognizer 移动 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4516661/

相关文章:

ios - 添加手势识别器以允许在 Swift 中的 MapView 上水平滑动

iphone - 第一次尝试动态过滤搜索数组

ios - 通用 Swift 结构 : Segmentation fault: 11

iphone - iOS开发: What are some reasons that [[self navigationController] viewControllers]; would return nil?

objective-c - IB 的 GestureRecognizers 在模拟内存警告时导致应用程序崩溃

iphone - Cocos2d 检测 UIGestureRecognizer 何时上下左右

iPhone核心数据: Property Persistance During Undo

iphone - 如何在 iPhone 设备库上创建文件夹/文件?

iphone - NSUserDefaults 不会保存 NSDictionary

objective-c - iPad : How to know Return key of iPad keyboard is pressed ? 请检查图像