【问题标题】:I'm having problems moving an programatically added UIIMAGEVIEW我在移动以编程方式添加的 UIIMAGEVIEW 时遇到问题
【发布时间】:2014-12-02 11:14:09
【问题描述】:

所以,我在使用 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 时,不会触发 touchesmoved 事件

【问题讨论】:

    标签: ios objective-c xcode uiimageview


    【解决方案1】:

    尝试设置p1.userInteractionEnabled = YES

    【讨论】:

      【解决方案2】:

      我看看这个:

      -(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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-15
        • 2012-07-17
        • 2017-05-25
        • 2012-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多