使用手势滑动视图

发布于 2024-11-05 06:38:31 字数 2852 浏览 4 评论 0原文

我正在尝试开发一个iPhone应用程序;但我是新手,所以我遇到了很多问题。

我的应用程序有一个默认视图和主视图中的另一个小视图,名称为抽认卡。

我想添加此视图滑动(或滑动)功能,就像 iPhone 中的照片一样。例如,如果用户滑动视图直到内部视图的中心到达主视图的边界,则将有两种可能性: 1-如果用户在到达内部视图之前完成滑动,将返回到原始位置。 2-如果用户不停止滑动,内部视图将从滑动方向退出屏幕并从相反方向返回屏幕。

因此,我在 viewDidLoad 中声明 UIPanGestureRecognizer,如下所示

UIPanGestureRecognizer *panFlashCard = [[UIPanGestureRecognizer alloc]
                                       initWithTarget:self action:@selector(handlePanFlashCard:)];

[flashcard addGestureRecognizer:panFlashCard];
[panFlashCard release];

handPanFlashCard 操作:

UIView *piece = [recognizer view];
    CGPoint center = piece.center;
    CGFloat x = 160; // the original x axis of inner view

    if ([recognizer state] == UIGestureRecognizerStateChanged) {
        CGPoint translation = [recognizer translationInView:piece ];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.55];
        [piece setCenter:CGPointMake(piece.center.x + translation.x, center.y)];
        [UIView commitAnimations];
        CGFloat totalX;
        totalX= x + translation.x;
        if(translation.x <0)
        {            
            if (totalX <= 0)
            {
                [self showNextCard];
            }
            else
            {
                [UIView beginAnimations:nil context:NULL];
                            [UIView setAnimationDuration:0.55];
                           [piece setCenter:CGPointMake(self.view.center.x, center.y)];
                           [UIView commitAnimations];
            }
        }
        else
        {
            if (totalX >= self.view.bounds.size.width)
            {
                [self showPreviousCard];
            }
            else
            {
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration:0.55];
                [piece setCenter:CGPointMake(self.view.center.x, center.y)];
                [UIView commitAnimations];
            }
        }
    }

showNextCard 操作:

[flashcard setCenter:CGPointMake(self.view.bounds.size.width + flashcard.bounds.size.width, flashcard.center.y)]; 

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.55];
    [flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)]; 
    [UIView commitAnimations];

showPreviousCard 操作:

  [flashcard setCenter:CGPointMake(0 - flashcard.bounds.size.width, flashcard.center.y)]; 

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.55];
    [flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)]; 
    [UIView commitAnimations];

当我运行此应用程序时,它会成功构建并运行,但动画有问题,我认为有一些帧错误。

请帮助我更正此代码并在滑动时运行流畅的动画。

I am trying to develop an application to Iphone; but i am new-bee so i have got a lot of problems.

My application has got a default view and another small view in the main view that name is flashcard.

I want to add capability this view swiping (or sliding) like Photos in Iphone. For example if user swipes view until the inner view's center reaches bounds of main view there will be two possibilities,
1- if user finish swiping before reach inner view will return to original position.
2- if user don't stop swiping, inner view will go out at screen from swipe direction and return the screen from the opposite direction.

So i declare UIPanGestureRecognizer at viewDidLoad like below

UIPanGestureRecognizer *panFlashCard = [[UIPanGestureRecognizer alloc]
                                       initWithTarget:self action:@selector(handlePanFlashCard:)];

[flashcard addGestureRecognizer:panFlashCard];
[panFlashCard release];

handPanFlashCard action:

UIView *piece = [recognizer view];
    CGPoint center = piece.center;
    CGFloat x = 160; // the original x axis of inner view

    if ([recognizer state] == UIGestureRecognizerStateChanged) {
        CGPoint translation = [recognizer translationInView:piece ];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.55];
        [piece setCenter:CGPointMake(piece.center.x + translation.x, center.y)];
        [UIView commitAnimations];
        CGFloat totalX;
        totalX= x + translation.x;
        if(translation.x <0)
        {            
            if (totalX <= 0)
            {
                [self showNextCard];
            }
            else
            {
                [UIView beginAnimations:nil context:NULL];
                            [UIView setAnimationDuration:0.55];
                           [piece setCenter:CGPointMake(self.view.center.x, center.y)];
                           [UIView commitAnimations];
            }
        }
        else
        {
            if (totalX >= self.view.bounds.size.width)
            {
                [self showPreviousCard];
            }
            else
            {
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration:0.55];
                [piece setCenter:CGPointMake(self.view.center.x, center.y)];
                [UIView commitAnimations];
            }
        }
    }

showNextCard action:

[flashcard setCenter:CGPointMake(self.view.bounds.size.width + flashcard.bounds.size.width, flashcard.center.y)]; 

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.55];
    [flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)]; 
    [UIView commitAnimations];

showPreviousCard action:

  [flashcard setCenter:CGPointMake(0 - flashcard.bounds.size.width, flashcard.center.y)]; 

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.55];
    [flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)]; 
    [UIView commitAnimations];

When i run this application, it builds and runs successfully but there is something wrong with animations, some frame mistakes i think.

Please help me to correct this code and run fluent animations while swiping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝在阴天搁浅。 2024-11-12 06:38:31

如果我明白你想要做什么,那么你可以使用内置控件来实现这一点。看一下 UIScrollView 类的 pagingEnabled 属性。还有一个名为 PhotoScroller 的示例项目可能会有所帮助。

http://developer.apple.com/library/ios/ #samplecode/PhotoScroller/Introduction/Intro.html

If I understand what you are trying to do then you can just use built in controls for this. Take a look at the pagingEnabled property on the UIScrollView class. There is also a sample project called PhotoScroller that may help.

http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文