在UIScrollView周围移动UIImage

时间:2014-10-02 23:44:54

标签: ios objective-c uiscrollview uiimage uigesturerecognizer

我的代码运行完美,但是当我将缩放添加到UIScrollView时,我无法在滚动条上移动图像,图像不响应事件拖动

我能做什么?

 --h file

#import <UIKit/UIKit.h>

@interface DragView : UIImageView
{
CGPoint previousLocation;

}

@end


--m file

#import "DragView.h"

@implementation DragView

- (id) initWithImage: (UIImage *) anImage
{
    if (self = [super initWithImage:anImage])
    {
        self.userInteractionEnabled = YES;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        self.gestureRecognizers = [NSArray arrayWithObjects:pan,nil];

    }

    return self;
}


// Promote touched view
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.superview bringSubviewToFront:self];
    previousLocation = self.center;

}


// Move view
- (void) handlePan: (UIPanGestureRecognizer *)uigr
{
    CGPoint translation = [uigr translationInView:uigr.view];
    uigr.view.center=CGPointMake(uigr.view.center.x+translation.x, uigr.view.center.y+ translation.y);
    [uigr setTranslation:CGPointMake(0, 0) inView:uigr.view];
}

@end

这里使用clase来创建图像。事件拖动

NSMutableArray *arregloImagenes;

DragView *mesas1;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    arregloImagenes=[[NSMutableArray alloc]init];


    mesas1 = [[DragView alloc]initWithImage:[UIImage imageNamed:@"mesa.png"]];
    mesas1.frame = CGRectMake(0, 0, 50, 50);
    [_propVistaCentral addSubview:mesas1];
    [_propVistaCentral bringSubviewToFront:mesas1];

    _scroll.minimumZoomScale=1;
    _scroll.maximumZoomScale=3;

}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

    return _propVistaCentral;

}

2 个答案:

答案 0 :(得分:0)

您可以尝试子类化UIScrollView并实现touchesShouldCancelInContentView:方法为DragView返回NO,否则返回YES,如下所示:

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
    return ([view class] != [DragView class]);
}

答案 1 :(得分:0)

我用这些属性解决了它

_scroll.delaysContentTouches=YES;
_scroll.canCancelContentTouches=NO;

因为y需要在进入滚动事件之前首先移动imagen