且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将UIImageView从UIscrollView拖动到另一个视图

更新时间:2022-10-17 17:46:27

这是另一种选择,使用 UIGraphicsGetImageFromCurrentImageContext()



您可以通过点击或从一些事件后。接下来,将其渲染为屏幕并应用平移手势来移动屏幕。

  UIView * _DraggableView; 
UIImageView * _imgvcChild1;

示例代码:

   - (void)onScrollviewClickOrOtherEvent 
{
UIGraphicsBeginImageContext(_DraggableView.frame.size);
[_DraggableView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * grab = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

if(_imgvcChild1)[_imgvcChild1 removeFromSuperview];
_imgvcChild1 = [[UIImageView alloc] initWithImage:grab];
_imgvcChild1.frame = _DraggableView.frame;
_imgvcChild1.userInteractionEnabled = YES;
[self.view addSubview:_imgvcChild1];

UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveObject :)];
[pan setMinimumNumberOfTouches:1];
[_imgvcChild1 addGestureRecognizer:pan];
}

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

---编辑--- p>

github上的示例项目: https:// github。 com / elpsk /购物车


I crawled all the web, but nothing.

I have a for loop to create uiimageview inside a uiscrollview controller. It's a collection of images that you can scroll horizontally only.

for (int i = 0; i < NIMAGES; i++) {
    NSString *filename = [NSString stringWithFormat:@"image%d.png", i+1];
    ScrollerImage *iv = [[ScrollerImage alloc] initWithNibName:@"ScrollerImage" bundle:nil];
    [iv initWithImage:[UIImage imageNamed:filename]];
    iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, 150.0f);
    iv.exclusiveTouch = YES;
    iv.userInteractionEnabled = YES;
    [sv addSubview:iv];
    [iv release];
}

The ScrollerImage class is an UIImageView controller

#import <UIKit/UIKit.h>
@interface ScrollerImage : UIImageView { 
}
@end

Now, how can I drag (not necessary a move, also a copy of view, or an id of view, anything that permit me to identify the image) an image from the scroller to another view? It's like a shopping cart, where you can drag items at the bottom view (cart).

Like this:

This is an alternative, using UIGraphicsGetImageFromCurrentImageContext()

You can grab an image from currentScreen on click or after some events. Next renderize it to screen and apply a Pan gesture to move around the screen.

UIView *_DraggableView;
UIImageView *_imgvcChild1;

sample code:

- ( void ) onScrollviewClickOrOtherEvent
{
  UIGraphicsBeginImageContext ( _DraggableView.frame.size );
  [ _DraggableView.layer renderInContext:UIGraphicsGetCurrentContext() ];

  UIImage *grab = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  if ( _imgvcChild1 ) [ _imgvcChild1 removeFromSuperview ];
  _imgvcChild1 = [[ UIImageView alloc ] initWithImage:grab ];
  _imgvcChild1.frame = _DraggableView.frame;
  _imgvcChild1.userInteractionEnabled = YES;
  [ self.view addSubview:_imgvcChild1 ];

  UIPanGestureRecognizer *pan = [[ UIPanGestureRecognizer alloc ] initWithTarget:self action:@selector(moveObject:) ];
  [ pan setMinimumNumberOfTouches:1 ];
  [ _imgvcChild1 addGestureRecognizer:pan ];
}

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

--- EDIT ---

A sample project on github: https://github.com/elpsk/Shopping-Cart