[英]Unable to use UIPanGestureRecognizer to move UIImageView
I am trying to create a program where a user can drag a UIImageView around the screen by using UIPanGestureRecognizer. 我正在尝试创建一个程序,用户可以使用UIPanGestureRecognizer在屏幕上拖动UIImageView。 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.
我的理解是UIImageView需要放在一个可以处理手势的UIView中。 In my program I have a view controller - AdvancedViewController1.
在我的程序中,我有一个视图控制器 - AdvancedViewController1。 I have created a UIView named AdvancedView1.
我创建了一个名为AdvancedView1的UIView。 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. 在界面生成器中,我已将AdvancedView1插入到AdvancedViewController 1中。以下是控制器和视图的
.h
和.m
文件。 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. 在IB中,我将IBOutlet与AdvancedView1链接到UIView,并将选项1链接到我希望能够移动的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];
}
You have to actually set the position of UIView in your pan: selector. 你必须在你的pan:selector中实际设置UIView的位置。 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.
而且由于您在AdvancedView类中而不是在其外部执行此操作,因此必须获取视图的父视图([self superview]),因为更新位置是相对于父(包含)视图。 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. 在初始化视图时,只需使用
[self setUserInteractionEnabled:YES]
将其打开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.