imageView点击 滑动事件

时间:2022-12-28 22:10:27

  imageView点击 滑动事件:

   UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(done)];

   [imageView addGestureRecognizer:tap];


 -(void) done
{
    int clickedIndex = 0;
    for (int i = 0; i < [self.buttonArray count]; i++)
    {
        if (touchView == [self.buttonArray objectAtIndex:i])
            clickedIndex = i;
    }
    
    if (clickedIndex < centerIndex)
    {
        for (int i = 0; i < centerIndex - clickedIndex; i++)
        {
            if ([self.buttonArray objectAtIndex:[self.buttonArray count] - 1] != nil)
            {
                [self.buttonArray insertObject:[self.buttonArray objectAtIndex:[self.buttonArray count] - 1] atIndex:0];
                [self.buttonArray removeObjectAtIndex:[self.buttonArray count] - 1 ];
                [self resetButtonFrame];
                
            }
            
        }
    }
    else if(clickedIndex > centerIndex)
    {
        for (int i = 0; i < clickedIndex - centerIndex; i++)
        {
            [self.buttonArray addObject:[self.buttonArray objectAtIndex:0]];
            [self.buttonArray removeObjectAtIndex:0];
            [self resetButtonFrame];
        }
    }
    else
    {
        int index = 0;
        for (int i = 0; i < [self.saveImageArray count]; i++)
        {
            if (touchView.image == [self.saveImageArray objectAtIndex:i])
                index = i;
        }
    }
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    touchView = (UIImageView*)[touch view];
}


-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint previousPoint = [touch previousLocationInView:self];
    CGPoint currentPoint = [touch locationInView:self];
    CGFloat x = currentPoint.x - previousPoint.x;
    if (currentPointCompare.x == previousPoint.x && currentPointCompare.y == previousPoint.y) {
        
    }else
    {
        if (x > 10)
        {
            [self.buttonArray insertObject:[self.buttonArray objectAtIndex:[self.buttonArray count] - 1] atIndex:0];
            [self.buttonArray removeObjectAtIndex:[self.buttonArray count] - 1 ];
            
        }else if(x < (-10))
        {
            [self.buttonArray addObject:[self.buttonArray objectAtIndex:0]];
            [self.buttonArray removeObjectAtIndex:0];
        }
        [self resetButtonFrame];
    }
    
    currentPointCompare = currentPoint;
}

子类的点击,滑动事件可通过self.nextResponder转到父类执行,如下
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesBegan:touches withEvent:event];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesMoved:touches withEvent:event];
}