子视图不响应ScrollView中的触摸

时间:2016-12-30 16:56:14

标签: ios objective-c uiview uiscrollview uiimageview

我有一个UIImageView和一个UIView(填充较小的图像视图)全部放在UIScrollView中(用于缩放)。

为了更好地帮助您理解,我在滚动视图中有一张带雾(ImageView,图片视图较小)的地图(UIView),因此我可以缩放地图。

如果我将mapview和fogview添加到self.view,那么应用程序就可以正常工作,触摸某处的雾会移除该雾图像。

但是,当我将地图和雾视图添加到滚动视图时,没有任何响应触摸。

我最终希望触摸雾视图的默认行为会在一点上消除雾。除非用户按下按钮来缩放/捏合视图,否则将缩放地图视图和雾视图。

下面的问题是当我将mapview和fogview添加到scrollview 时,不会处理任何触摸。目前无法按下按钮进行捏合/缩放,因为我仍然没有我想要的默认行为。

任何帮助?

@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, UIGestureRecognizerDelegate, UIScrollViewDelegate>{
    UIImage * fogImage;
    NSUserDefaults * defaults;
    bool canZoom;
}
@property (weak, nonatomic) IBOutlet UIButton *btnTakePicture;

@property (strong, nonatomic) UIButton * btnMenu;
@property (strong, nonatomic) UIButton * btnZoom;

@property (strong, nonatomic) UIScrollView* scrollView;
@property (strong, nonatomic) UIImageView *mapView;
@property (strong, nonatomic) UIView * fogView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    fogImage = [UIImage imageNamed:@"fog1.jpg"];

    self.mapView = [[UIImageView alloc] initWithFrame:CGRectZero];
    self.fogView = [[UIView alloc] initWithFrame:CGRectZero];

    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    _scrollView.delegate = self;
    _scrollView.minimumZoomScale = 0.75;
    _scrollView.maximumZoomScale = 3.0;

    [_scrollView addSubview:self.mapView];
    [_scrollView addSubview:self.fogView];
    [self.view addSubview:_scrollView];

    self.mapView.hidden = YES;
    self.fogView.hidden = YES;
    self.scrollView.hidden = YES;


    defaults = [NSUserDefaults standardUserDefaults];

    canZoom = false;


}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    self.mapView.frame      = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.fogView.frame      = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.scrollView.frame   = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}


- (void) zoom:(id)sender{
    canZoom = !canZoom;
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
    self.btnTakePicture.hidden = YES;

    self.btnMenu.hidden = NO;
    self.btnZoom.hidden = NO;

    [picker dismissViewControllerAnimated:YES completion:^(void) {
        [self fillScreenWithFog];
        self.mapView.image = chosenImage;
        self.mapView.contentMode = UIViewContentModeScaleAspectFit;
        self.mapView.hidden = NO;
        self.fogView.hidden = NO;
        _scrollView.hidden = NO;
        _scrollView.userInteractionEnabled = YES;
        self.scrollView.contentSize = self.mapView.frame.size;

    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}


#pragma mark Fog Maker

- (void) removeFogAtPoint:(CGPoint)point{
    NSLog(@"removeFogAtPoint %@", NSStringFromCGPoint(point));
    CGRect fingerRect = CGRectMake(point.x - 5, point.y-5, 10, 10);

    for(UIImageView *view in self.fogView.subviews){
        CGRect subviewFrame = view.frame;

        if(CGRectIntersectsRect(fingerRect, subviewFrame)){
            [UIView animateWithDuration:1.25
                             animations:^{
                                    view.alpha = 0;
                                }
                             completion:^(BOOL finished){
                                 [view removeFromSuperview];

                             }];
        }

    }
}

- (void) fillScreenWithFog {
    // do a loop to fill the screen with fog getNewSquare

}

- (UIImageView*) getNewSquare:(CGRect)frame withTag:(int)tag{
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = fogImage;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    return imageView;

}

-(void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touches Moved");
    CGPoint location = [[touches anyObject] locationInView:self.scrollView];
    [self removeFogAtPoint:location];

}

@end

0 个答案:

没有答案