How to move shape on screen?

Discussion in 'iOS Programming' started by mikezang, Oct 28, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I found iCardSort and I can move card on screen when I tap that card, I want to know how can I do it in my application, does anyone give me some hint or tips?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    Thanks for your advise.
     
  4. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #4
    I made a table view and I have two questions.

    1. Can I set background image to transparency in circle of attachment?

    2. I used code as below to move that table view, but it never moved, can you help me?
    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	
    	table = [[UITableView alloc] initWithFrame:CGRectMake(100, 100, 256, 256)];
    	table.multipleTouchEnabled = YES;
    	table.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"icon.png"]];
    
    	[self.view addSubview:table];
    }
    
    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    	UITouch *touch = [[event allTouches] anyObject];
    	CGPoint location = [touch locationInView:touch.view];
    	table.center = location;
    }
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    	[self touchesBegan:touches withEvent:event];
    }
    
     

    Attached Files:

  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    1) Probably. I think you need to turn off opaque.

    2) Few comments. Table views are scroll views and are probably quite complicated to make this work, if at all possible. I don't know. If you override one of the touches methods you need to override all four. Obviously a table view/scroll view needs to get its touches in order to work correctly so you'll need to pass the touches (or some of them) to the table view. You might also look at UIGestureRecognizer to implement this. The moveme sample code was written before gesture recognizers existed and using the touches methods is a bit old-fashioned. At any rate making a table view follow your finger is going to be more complicated than making a simple view follow your finger.

    I'm not sure that the logic in your touchesBegan method is correct. Are you sure that the touches methods are being called? Can you log the location and see if it's what you expect? Are the touches methods in your view controller? If so I'm not sure that the table view will call them. They probably need to be in a tableview subclass. Using the gesture recognizers will avoid having to subclass the table view.
     
  6. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #6
    Do you mean that table view's opaque? I off it, but the result is the same.

    I just want check if I can move it so that I selected simple way, like you said I will try gesture.

    By the way, I found I can move it when I clicked out of table view, do you know why?
     

    Attached Files:

  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    My guess is that you have added the touchesBegan to your view controller. The tableview already has its own touchesBegan method. If you touch the background view then the touch is sent to the view controller but if you touch the table the table swallows the touch. Not sure though.

    Did you set the background color of the table to clearColor also? You might also need to set the backgroundView to nil.
     
  8. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #8
    Your explain might be right.

    backgroundView = nil doesn't work.
    I also used code as below so that I can't use clearColor.
    Code:
    table.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"icon.png"]];
     
  9. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #9
    I got what I need with gestureRecognizer as below
    Code:
    	
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [table addGestureRecognizer:panGesture];
    [panGesture release];
    But trans is still not clear:)
     
  10. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    I think you need transparency in your image. Is that corner black or transparent? If it is transparent then I think there may be a problem with using the pattern color. Instead you could set the backgroundView to an image view with this image in it.
     
  11. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #11
    It is transparency.
     

    Attached Files:

    • icon.png
      icon.png
      File size:
      83.8 KB
      Views:
      239
  12. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    How is the image displayed on screen? Is it in a view of it's own (say a UIImageView)? If so what is the background colour of the view? Is the view set to display it's background? What have you tried to solve this already?
     
  13. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #13
    Well, I used code as below to get transparency background image.
    Code:
    	
    ...
    table = [[UITableView alloc] initWithFrame:CGRectMake(100, 100, 256, 256)];
    table.backgroundColor = [UIColor clearColor];
    UIImage *back = [UIImage imageNamed:@"icon.png"];
    UIImageView *backgroundView = [[UIImageView alloc] initWithImage:back];
    table.backgroundView = backgroundView;
    [backgroundView release];
    [self.view addSubview:table];
    ...
    
     

    Attached Files:

  14. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #14
    Okey Dokey. Don't know if this is a bug in the pattern color or that's how it's supposed to work.
     

Share This Page