Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我正在尝试向uiimageview子类添加UIPangestureRecognitor,但它不起作用_Ios_Uiimageview_Subclass_Uipangesturerecognizer - Fatal编程技术网

Ios 我正在尝试向uiimageview子类添加UIPangestureRecognitor,但它不起作用

Ios 我正在尝试向uiimageview子类添加UIPangestureRecognitor,但它不起作用,ios,uiimageview,subclass,uipangesturerecognizer,Ios,Uiimageview,Subclass,Uipangesturerecognizer,这是我的代码,第一部分是我要实例化的ImageView的自定义子类 下一部分是viewController.m viewdidload 我创建了子类,这样每个imageview都有自己的UIPangestureRecognitor,而不是在viewcontroller.h中创建许多不同的UIPangestureRecognitor。我认为这是一种更有效、更具创造性的做事方式,但我似乎无法移动ImageView 提前谢谢你的帮助 #import "blogsImageView.h" @imple

这是我的代码,第一部分是我要实例化的ImageView的自定义子类 下一部分是viewController.m viewdidload 我创建了子类,这样每个imageview都有自己的UIPangestureRecognitor,而不是在viewcontroller.h中创建许多不同的UIPangestureRecognitor。我认为这是一种更有效、更具创造性的做事方式,但我似乎无法移动ImageView

提前谢谢你的帮助

#import "blogsImageView.h"

@implementation blogsImageView

- (id)initWithFrame:(CGRect)frame
{
   //custom imageview subclass
   self = [super initWithFrame:frame];
    if (self) {
    NSLog(@"Test");
    [self setMultipleTouchEnabled:YES];
    [self setUserInteractionEnabled:YES];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
    panRecognizer.delegate = self;

    [self addGestureRecognizer:panRecognizer];
}
return self;
}

- (void) panDetected:(UIPanGestureRecognizer *)panRecognizer{

NSLog(@"Test");
if (panRecognizer.state == UIGestureRecognizerStateBegan)
{

}
else if (panRecognizer.state == UIGestureRecognizerStateChanged)
{
    CGPoint translation = [panRecognizer translationInView:self.superview];
    CGPoint imageViewPosition = self.center;
    imageViewPosition.x += translation.x;
    imageViewPosition.y += translation.y;

    self.center = imageViewPosition;
    [panRecognizer setTranslation:CGPointZero inView: self.superview];

}
else if (panRecognizer.state == UIGestureRecognizerStateEnded)
{

}

}

//my viewController file
- (void)viewDidLoad
{
[super viewDidLoad];

blogsImageView *testImageView = (blogsImageView *)[[UIImageView alloc]initWithFrame:CGRectMake(20, 300, 80, 80)];
[testImageView setImage:[UIImage imageNamed:@"testImage"]];
[self.view addSubview:testImageView];

}
这条线,

blogsImageView *testImageView = (blogsImageView *)[[UIImageView alloc]initWithFrame:CGRectMake(20, 300, 80, 80)];
这是不正确的。将UIView强制转换为blogsImageView并不能使其成为一个UIView。你需要alloc init你的子类

blogsImageView *testImageView = [[blogsImageView alloc]initWithFrame:CGRectMake(20, 300, 80, 80)];

谢谢,这正是我所需要的,Idk,为什么我厌倦了投it@brianScroggins,你应该接受我的回答,因为它解决了你的问题。