【问题标题】:Tinder-Like Swipe Animation for iOS [closed]适用于 iOS 的类似 Tinder 的滑动动画 [关闭]
【发布时间】:2014-04-04 03:23:37
【问题描述】:

我已经按照这个非常有用的教程来了解如何添加类似 tinder 的滑动 (http://guti.in/articles/creating-tinder-like-animations/);但是,我有一个问题 - 当图片消失时,我想用另一张图片替换它。我该怎么做/在哪里做?

【问题讨论】:

  • 最简单的做法是在第一个视图下方添加另一个可拖动视图。然后,您只需在它们之间交替。 (在拖动第一个时,第二个坐在原位,直到第一个被拖走,反之亦然)。所以 2 个相同的视图,您只需交替拖动哪个视图和隐藏哪个视图。
  • github.com/modocache/MDCSwipeToChoose 这可能会为您节省一些时间。

标签: ios objective-c uiview swipe uipangesturerecognizer


【解决方案1】:

我根据该教程构建了一个更简单、更全面的项目。特别是,您可以使用图像数组并根据图像的索引为每张卡片分配一个图像。图像是动态的还是静态的?

此时可能无济于事,但无论如何: https://github.com/cwRichardKim/TinderSimpleSwipeCards

【讨论】:

    【解决方案2】:

    看看这个,用swift 4写的

    https://github.com/nickypatson/TinderSwipeView

    func beingDragged(_gestureRecognizer: UIPanGestureRecognizer) {

        xFromCenter = gestureRecognizer.translation(in: self).x
        yFromCenter = gestureRecognizer.translation(in: self).y
        switch gestureRecognizer.state {
        //%%% just started swiping
        case .began:
            originalPoint = self.center;
            break;
    
        //%%% in the middle of a swipe
        case .changed:
            let rotationStrength = min(xFromCenter / ROTATION_STRENGTH, ROTATION_MAX)
            let rotationAngel = .pi/8 * rotationStrength
            let scale = max(1 - fabs(rotationStrength) / SCALE_STRENGTH, SCALE_MAX)
            center = CGPoint(x: originalPoint.x + xFromCenter, y: originalPoint.y + yFromCenter)
            let transforms = CGAffineTransform(rotationAngle: rotationAngel)
            let scaleTransform: CGAffineTransform = transforms.scaledBy(x: scale, y: scale)
            self.transform = scaleTransform
            updateOverlay(xFromCenter)
            break;
    
        case .ended:
            afterSwipeAction()
            break;
    
        case .possible:break
        case .cancelled:break
        case .failed:break
        }
    }
    

    让 panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.beingDragged)) addGestureRecognizer(panGestureRecognizer)

    希望这会奏效。让我知道

    谢谢

    【讨论】:

      【解决方案3】:

      此答案基于 cwRichardKim 的代码/回复(感谢 cwRichardKim!)。我没有找到如何将图像添加到每张卡片的指南。在下面粘贴我的方法(在此示例中,图像 url 是硬编码的,但在实际应用中可以从云中获取图像 url):

      在 DraggableView.h 中:

      @property (nonatomic,strong)UIImageView* photoImageView; //%%% a placeholder for member photo to place on card
      

      在 DraggableView.m 中:

      ...
      @synthesize photoImageView;
      ... 
      // - (id)initWithFrame:(CGRect)frame ...
      // add photo to card
              //You need to specify the frame of the view
              UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.width)];
              photoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"place_holder_image.png"]];
              // UIImageView *imageView = [[UIImageView alloc] initWithImage:photo];
              //specify the frame of the imageView in the superview , here it will fill the superview
              photoImageView.frame = photoView.bounds;
              // add the imageview to the superview
              [photoView addSubview:photoImageView];
              //add the view to the main view
              [self addSubview:photoView];
      

      在 DraggableViewBackground.h 中:

      @property(retain,nonatomic)NSArray* exampleCardUrls;
      

      在 DraggableViewBackground.m 中:

      ...
      @synthesize exampleCardUrls;
      ...
      exampleCardUrls = [[NSArray alloc]initWithObjects:@"http://images.clipartpanda.com/clipart-people-nTBX8zkgc.jpeg",@"http://epilepsyu.com/wp-content/uploads/2014/01/happy-people.jpg",@"http://alivecampus.com/wp-content/uploads/2014/09/people-02.jpg",@"https://www.google.com/images/srpr/logo11w.png",@"http://modalpoint.com/wp-content/uploads/2014/11/people-blue-stick-people-hi.png", nil];
      ...
      // ... -(DraggableView *)createDraggableViewWithDataAtIndex:(NSInteger)index ...
      // retrieve image for cell in background
          NSURL *url = [NSURL URLWithString:exampleCardUrls[index]];
          [self loadImage:url withIndex:index];
      ...
      #pragma mark - cloud, load image
      - (void)loadImage:(NSURL *)imageURL withIndex:(NSInteger)index
      {
          // create array of objects to pass to next method
          NSMutableArray* params = [[NSMutableArray alloc]init];
          [params addObject:imageURL];
          NSNumber *indexNumber = [NSNumber numberWithInt:index];
          [params addObject:indexNumber];
      
          NSOperationQueue *queue = [NSOperationQueue new];
          NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                              initWithTarget:self
                                              selector:@selector(requestRemoteImage:)
                                              object:params];
          [queue addOperation:operation];
      }
      
      - (void)requestRemoteImage:(NSMutableArray*)params
      {
          // get data from params
          NSURL* imageURL = params[0];
      
          NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
          UIImage *image = [[UIImage alloc] initWithData:imageData];
          params[0] = image;
      
          [self performSelectorOnMainThread:@selector(placeImageInUI:) withObject:params waitUntilDone:YES];
      }
      
      - (void)placeImageInUI:(NSArray*)params
      {
          // get data from params
          UIImage* image = params[0];
          NSNumber* indexNumber = params[1];
          NSInteger index = [indexNumber integerValue];
      
          DraggableView* myDraggableView = allCards[index];
          myDraggableView.photoImageView.image = image;
          allCards[index] = myDraggableView;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-22
        • 2015-09-23
        • 1970-01-01
        • 1970-01-01
        • 2020-09-18
        • 2014-10-31
        • 1970-01-01
        • 2012-11-09
        相关资源
        最近更新 更多