2

大家好,我有一个UIViewControllerUIImageView,我有一个SwipeGestureRecognizer。因此,如果用户向左或向右滑动,它将图像changeUIImageView到目前为止它的工作。UIScrollView在启用分页的情况下,我不能使用其中一个图像的大小。我想实现拖动的效果,并UIImageViewUIScrollView图像中那样查看下一部分。

在此处输入图像描述

4

6 回答 6

3

简单的回答:

为图像视图添加两个手势并放置一个计数变量,根据滑动和否切换计数变量。的图像。最后根据计数更改图像。

使用此代码也可以正常工作且简单。

- (void)addAnimationPresentToView:(UIView *)viewTobeAnimated
{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.30;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [transition setValue:@"IntroSwipeIn" forKey:@"IntroAnimation"];
    transition.fillMode=kCAFillModeForwards;
    transition.type = kCATransitionPush;
    transition.subtype =kCATransitionFromRight;
    [viewTobeAnimated.layer addAnimation:transition forKey:nil];

}

- (void)addAnimationPresentToViewOut:(UIView *)viewTobeAnimated
{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.30;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [transition setValue:@"IntroSwipeIn" forKey:@"IntroAnimation"];
    transition.fillMode=kCAFillModeForwards;
    transition.type = kCATransitionPush;
    transition.subtype =kCATransitionFromLeft;
    [viewTobeAnimated.layer addAnimation:transition forKey:nil];

}
                - (void)viewDidLoad
                {
                    [super viewDidLoad];
                    count = 0;
                    // Do any additional setup after loading the view from its nib.
                    [self.imageViewSwipe setUserInteractionEnabled:YES];
                    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
                    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];

                    // Setting the swipe direction.
                    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
                    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

                    // Adding the swipe gesture on image view
                    [self.imageViewSwipe addGestureRecognizer:swipeLeft];
                    [self.imageViewSwipe addGestureRecognizer:swipeRight];


                }
                -(void)changeImage
                {
                    switch (count) {
                        case 1:
                            self.imageViewSwipe.image = [UIImage imageNamed:@"Screen Shot 2013-09-17 at 10.57.51 AM.png"];
                            break;
                        case 2:
                            self.imageViewSwipe.image = [UIImage imageNamed:@"Screen Shot 2013-09-17 at 10.58.04 AM.png"];
                            break;
                        case 3:
                            self.imageViewSwipe.image = [UIImage imageNamed:@"Screen Shot 2013-09-17 at 10.58.00 AM.png"];
                            break;

                        default:
                            break;
                    }
                }
                -(void)handleSwipe:(id)sender
                {
                 if(count < 4)
                 {
                     count++;
                     UIImageView *moveIMageView = self.imageViewSwipe;
                     [self addAnimationPresentToView:moveIMageView];
                     [self changeImage];
                 }

                }
                -(void)handleSwipeRight:(id)sender
                {
                    if(count>0)
                    {
                        count--;
                  UIImageView *moveIMageView = self.imageViewSwipe;
                 [self addAnimationPresentToViewOut:moveIMageView];
                        [self changeImage];
                    }

                }
                    -(void)handleSwipe:(id)sender
                    {
                     if(count < 4)
                     {
                         count++;
                         [self changeImage];
                     }

                    }
                    -(void)handleSwipeRight:(id)sender
                    {
                        if(count>0)
                        {
                            count--;
                            [self changeImage];
                        }

                    }
于 2013-09-20T09:14:42.050 回答
2

将滚动视图添加到主视图(适当设置框架)

[mainview addSubview:scr];

写一个方法,如:

- (void)setupScrollView:(UIScrollView*)scrMain {
}

然后viewDidLoad将滚动视图传递给方法,

 [self setupScrollView:scr];



- (void)setupScrollView:(UIScrollView*)scrMain {
    // we have 10 images here.
    // we will add all images into a scrollView & set the appropriate size.

    for (int i=1; i<=5; i++) {
        // create image
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.png",i]];
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.png",i]];
        // create imageView
//        NSLog(@"===============>%d",i);
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];
        // set scale to fill
        imgV.contentMode=UIViewContentModeScaleToFill;
        // set image
        [imgV setImage:image];
        // apply tag to access in future
        imgV.tag=i+1;
        // add to scrollView
        [scrMain addSubview:imgV];
    }
    // set the content size to 10 image width
    [scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*5, scrMain.frame.size.height)];
    // enable timer after each 2 seconds for scrolling.
    // [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:nil userInfo:nil repeats:YES];
}

它使您可以手动在两个方向上移动。

于 2013-09-20T09:01:13.823 回答
1

*您可以尝试使用 imageView.layer 来提供一些动画效果。 *

于 2013-09-20T08:59:02.620 回答
1

我认为最好的是使用UIPanGestureRecognizer

UIPanGestureRecognizer * pan1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(moveObject:)];
pan1.minimumNumberOfTouches = 1;
[image1 addGestureRecognizer:pan1];

-(void)moveObject:(UIPanGestureRecognizer *)pan;
{
 image1.center = [pan locationInView:image1.superview];
}

当您开始拖动时,以某种方式检查方向。如果方向是从左到右,则使用 -image.width 将“之前”图像添加到屏幕,并在 move 方法中移动此 imageBefore.center = [pan locationInView:image1.superview]; 也。如果方向是从右到左,则使用 +screensize+image.width 将“下一个”图像添加到 scree,并在 move 方法中移动此 imageNext.center = [pan locationInView:image1.superview]; 也。

我还没有尝试过,但我认为它应该可以工作。我希望它有帮助

编辑:

我为您找到了如何检测方向:

-(void)moveObject:(UIPanGestureRecognizer *)pan;
{

    CGPoint vel = [pan velocityInView:self.view];
    if (vel.x > 0)
    {
        // user dragged towards the right
        counter++;
    }  
    else
    {
        // user dragged towards the left
        counter--;
    }
}
于 2013-09-20T09:04:01.383 回答
0

实现水平表格滚动

创建一个 TableView 并转换表格

self.horizontalTableView.transform = CGAffineTransformMakeRotation(-M_PI * 0.5);

然后,您将转换反向应用于单元格内的内容。

CGAffineTransformMakeRotation(M_PI * 0.5)

如果需要,您可以将其放入自定义 UITableViewCell 内的另一个 TableView 中,并有一个水平表的垂直表。

您将获得本机滚动的所有好处

请参阅本教程:

http://www.raywenderlich.com/4723/how-to-make-an-interface-with-horizo​​ntal-tables-like-the-pulse-news-app-part-2

于 2014-05-11T23:01:42.190 回答
0
-(void)panTableView:(UISwipeGestureRecognizer *)pan{
    if (pan.state==UIGestureRecognizerStateEnded) {
        int curr=currentPage;

        if (pan==self.panGestureLeft)
        {
            // user dragged towards the right

                CATransition *transition = [CATransition animation];
                transition.duration = 0.35;
                transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                transition.type = kCATransitionPush;
                transition.subtype =kCATransitionFromRight;
                transition.delegate = self;
                [scrollview.layer addAnimation:transition forKey:nil];


        }
        else if (pan==self.panGestureRight)
        {
            // user dragged towards the left

                CATransition *transition = [CATransition animation];
                transition.duration = 0.35;
                transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                transition.type = kCATransitionPush;
                transition.subtype =kCATransitionFromLeft;
                transition.delegate = self;
                [scrollview.layer addAnimation:transition forKey:nil];


        }



}

}

于 2013-09-20T09:35:58.163 回答