iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 无法使用UIPanGestureRecognizer移动UIImageView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个程序,用户可以使用UIPanGestureRecognizer在屏幕上拖动UI ImageView.我尝试了几种不同的方法,但无法弄明白.我不确定是否需要创建发件人/请求者.我的理解是UIImageView需要放在一个可以处理手势的UIView中.在我的程序中,@L_944_5@一个视图控制器 – 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,并将选项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];
}

解决方法

你必须在你的pan:SELEctor中实际设置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]将其打开.

大佬总结

以上是大佬教程为你收集整理的iphone – 无法使用UIPanGestureRecognizer移动UIImageView全部内容,希望文章能够帮你解决iphone – 无法使用UIPanGestureRecognizer移动UIImageView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。