简体   繁体   中英

Dismiss UIScrollView based on touch coordinates on UIImageView embedded into the UIScrollView

There is a UITableView with many photos. As a photo is tapped, a modalView is presented with UIScrollView , which further has an UIImageView embedded into it. The tapped image is presented here (similar to the Facebook app). Now, i can move the image vertically using touchesMoved and CGRectMake . What i want to achieve is that when i drag the image vertically, on reaching a certain threshold, the ImageView & ScrollView should disappear and we should be back to the tableView.

Image moved using:

- (void) touchesMoved:(NSSet *)touches
        withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
    NSLog(@"%f", pos.y);

if (pos.y>50&&pos.y<430){
    [UIView setAnimationDelay:0];

    [UIView animateWithDuration:0.4

                     animations:^{_photoImageView.frame=CGRectMake(0,pos.y-100,320,200);}

                     completion:^(BOOL finished){ }];
}
}

The action to return control to the tableView is needed here:

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

UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];

if (pos.y>50&&pos.y<430){
    [UIView setAnimationDelay:0];

    [UIView animateWithDuration:0.4

                     animations:^{_photoImageView.frame=CGRectMake(0,140,320,200);}

                     completion:^(BOOL finished){ }];
}
else if(pos.y<50||pos.y>430)
    //Code needed here !!
    ;
}

Further, if transparency can be achieved on the imageView background (ie Scrollview) such that the table view is visible with certain Alpha, that would be highly appreciated.

Dismiss with dismissModalViewControllerAnimated: (should use the newer method dismissViewControllerAnimated:completion: ).

(This was assuming you presented with presentModalViewController:animated: , just adding a view to the screen is not a modal presentation). If you didn't really present a modal then just use removeFromSuperview .

When presenting a view modally the view below will be removed from display (it's expected not to be visible), so if you make the modal view transparent you will just see black below it. If you want to make the overlay view transparent you should just make it a subview, set the opacity ( alpha ) and use bringSubviewToFront: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM