0

I want an object to be created when I touch an UIView. But I want the new object to be able to moved without the finger having to be raised. I tried passing the touched event to the new object but it doesn't work.

Is there any way to do it?

Josh Lee
  • 29
  • 4

2 Answers2

0

You'll have to derive a subclass of the UIView, which will serve as a HolderView for all newly generated views. Also, once a new view is generated, the new view will move along with the finger even if its not raised.

Following code will do it, tweak it later as per your needs:

@interface HolderView : UIView

@property(nonatomic,retain)UIView *newView;

@end

@implementation HolderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];

    if (CGRectContainsPoint(self.bounds, touchPoint)) {

        CGRect r = CGRectMake(touchPoint.x-50, touchPoint.y-50, 100, 100);

        self.newView = [[[UIView alloc] initWithFrame:r]autorelease];
        self.newView.backgroundColor= [UIColor cyanColor];
        [self addSubview:self.newView];

    }
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.newView!=nil) {
        UITouch *touch = [touches anyObject];
        CGPoint touchPoint = [touch locationInView:self];
        self.newView.center = touchPoint;
    }
}

@end
CodenameLambda1
  • 1,299
  • 7
  • 17
0

i think the touch is creating a problem at your end while you touch at some other object the view is also present so touch can not identified on your movable object for that you need to use one UIImageview over your UIView and then detect the touch and then you'll be able to achieve your desired output

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint imgPop = [touch locationInView:imgPopup];
    if ([imgPopup pointInside:imgPop withEvent:event])
    {
        CGPoint imgReply = [touch locationInView:viewComment];
        if ([viewComment pointInside:imgReply withEvent:event])
        {

        } else {
            viewPopup.hidden = TRUE;
        }        
    }
}

this is how i manage this code is for dismissing the view on touch but you can modify and use for your purpose.

D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30