gpt4 book ai didi

iphone - 在 UIScrollView 上拖动图像

转载 作者:行者123 更新时间:2023-11-29 13:14:01 24 4
gpt4 key购买 nike

当用户触摸 UITableViewCell 上的图像时,将调用 UIViewController。它是使用 modalViewController 调用的。在这个 modalViewController 上是一个 UIScrollView,中间有另一个 UIImageView,它使用 NSDictionary 获取图像(通过来自 UITableViewCell)。

现在,我想要实现的是用户只能垂直拖动图像的能力,这样拖动和释放一点就会导致图像返回中心并带有动画。如果用户将它拖到极限,整个 UIScrollView 将被关闭,用户返回到 UITableView。我使用了以下代码。这里的问题是,正如我的名字所暗示的那样,这段代码很粗糙。有没有一种不需要那么多计算的优雅方法?

BOOL imageMoved=NO;

- (void) touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];

CGRect imageRect = _photoImageView.frame;//_photoImageView object of UIImageView
CGFloat imageHeight = imageRect.size.height;//getting height of the image
CGFloat imageTop=240-imageHeight/2;
CGFloat imageBottom=imageTop+imageHeight;//setting boundaries for getting coordinates of touch.
// Touches that originate above imageTop and below imageBottom are ignored(until touch reaches UIImageView)

if (pos.y>50&&pos.y<430&&pos.y>=imageTop&&pos.y<=imageBottom){//extremes are defined as top and bottom 50 pixels.
imagedMoved=YES;//BOOL variable to hold if the image was dragged or not
NSLog(@"%f", pos.y);
[UIView setAnimationDelay:0];

[UIView animateWithDuration:0.4

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

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

- (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){//if touch has NOT ended in the extreme 50 pixels vertically
[UIView setAnimationDelay:0];//restoring UIImageView to original center with animation

[UIView animateWithDuration:0.4

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

completion:^(BOOL finished){ }];
imagedMoved=NO;//prepare BOOL value for next touch event
}

else if(pos.y<50||pos.y>430)
if(imagedMoved)
[self.photoBrowser exit] ;//exit function(imported from UIScrollViewController) dismisses
//modalView using [self dismissViewControllerAnimated:YES completion:nil];
}

这里的所有代码都是对 MWPhotoBrowserUITapView 的自定义副本的修改。 .

最佳答案

哟,这里有一个更简单的方法来做到这一点,或多或少是亚历山德罗所说的一个例子。我没有找到屏幕顶部,但我给出了它的错觉。

BCViewController.h

#import <UIKit/UIKit.h>

@interface BCViewController : UIViewController <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *svScroller;
@property (weak, nonatomic) IBOutlet UIImageView *ivFish;

@end




#import "BCViewController.h"

@interface BCViewController (){
UIPanGestureRecognizer *_panGestureRecognizer;
CGPoint _fishOrigin;
}

@end

@implementation BCViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.svScroller.delegate = self;
[self.svScroller setContentSize:self.view.frame.size];
self.svScroller.translatesAutoresizingMaskIntoConstraints = NO;
self.ivFish.userInteractionEnabled = YES;

_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[self.ivFish addGestureRecognizer:_panGestureRecognizer];

_fishOrigin = self.ivFish.center;

NSLog(@"center %f", self.ivFish.center.x);
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)handlePanFrom:(UIPanGestureRecognizer *)recognizer{
// if you want it but not used
CGPoint translation = [recognizer translationInView:recognizer.view];
// if you want it but not used
CGPoint velocity = [recognizer velocityInView:recognizer.view];
CGPoint tempPoint;


if (recognizer.state == UIGestureRecognizerStateBegan) {

} else if (recognizer.state == UIGestureRecognizerStateChanged) {
tempPoint = [recognizer locationInView:self.view];

self.ivFish.center = CGPointMake(175.5, tempPoint.y);

} else if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint tempPoint;
tempPoint = [recognizer locationInView:self.view];

if (tempPoint.y < 132) {
[UIView animateWithDuration:.3 delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^ {
[self.navigationController popViewControllerAnimated:TRUE];
}
completion:NULL];
} else {
[UIView animateWithDuration:.3 delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^ {
self.ivFish.center = _fishOrigin;
}
completion:NULL];
}
}
}

关于iphone - 在 UIScrollView 上拖动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16324241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com