我在移动以编程方式添加的UIIMAGEVIEW时遇到问题

时间:2014-12-02 11:14:10

标签: ios objective-c xcode uiimageview

所以,我在使用touchesBegan和touchesMoved移动一些图像时遇到问题这里是p1 UIImageView的代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];

    CGPoint location = [touch locationInView: touch.view];


    NSLog(@"incepe");

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];

    if (touch.view == self.p1) {

        CGPoint location = [touch locationInView: self.view];

        self.p1.center = location;
        NSLog(@"nmk");
    }

}

和我添加图片的方式是这个

-(void)img{
    if (!self.p1) {
        self.p1 =[[UIImageView alloc] initWithFrame:CGRectMake(14,20,70,50)];
    }
    [self.view addSubview:self.p1]; 
}

当我按下p1 UIImageview

时,触摸移动事件不会被触发

2 个答案:

答案 0 :(得分:0)

尝试设置p1.userInteractionEnabled = YES

答案 1 :(得分:0)

我看看这个:

-(void)viewDidAppear:(BOOL)animated{
    UIPanGestureRecognizer *pgr1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(image1Moved:)];
    [yourImageView addGestureRecognizer:pgr1];

}

-(void)image1Moved:(UIPanGestureRecognizer *)pgr{
    NSLog(@"Came here");
     [yourImageView setCenter:[pgr locationInView:self.view]];
}

P.S yourImageView应该userInteractionEnabledYES