March 29, 2010 4:46:33 AM PDT (23 weeks ago). Seen 535 times.
Photo Yoyo Zhao
Fontys
Member since Mar 29, 2010
Posts: 3
I having problem to get image parsing by XML from web into a scroll view on iPhone app without interface builder, can u give me an example
Edited one time. Last edit by Yoyo Zhao on Mar 29, 2010 at 4:46:33 AM (about 15 weeks ago).
March 29, 2010 8:56:30 AM PDT (23 weeks ago)
Photo Jonathan Lehr
President
AboutObjects
Member since Mar 12, 2009
Location: South Riding
Posts: 22
I'm not clear on what you're trying to do. Can you be more specific?

- Jonathan
March 30, 2010 2:48:12 AM PDT (23 weeks ago)
Photo Yoyo Zhao
Fontys
Member since Mar 29, 2010
Posts: 3
sorry for my bad english, i ready solved this problem, but i have another problems

i want to add a button with click function inside a scrollview (something like a sliding menu), and also can click do the touch even with the image (image will change when u touch it)


Code:
scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.contentSize = CGSizeMake(1024, 768);
scrollView.bounces = NO;
scrollView.delaysContentTouches = NO;

NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"gallery.png"]];
image = [[UIImage alloc] initWithData:imageData];
imageDown = [UIImage imageNamed:@"clock.png"];
imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = YES;
[scrollView addSubview:imageView];

button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
button.frame = CGRectMake(150, 150, 128, 128);
button.bounds = CGRectMake(0, 0, 128.0, 128.0);
[button setImage:[UIImage imageNamed:@"clock.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];

[self.view addSubview:scrollView];


Code:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == imageView) {
imageView.image = imageDown;
}
[super touchesBegan:touches withEvent:event];
[self setStartTouchPosition:[touch locationInView:self]];
}

- (BOOL) isTouchMoving: (UITouch *)touch {
CGPoint currentTouchPosition = [touch locationInView:self];
BOOL rValue = NO;

if (fabsf([self startTouchPosition].x - currentTouchPosition.x) >=2.0) {
rValue = YES;
}
return (rValue);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if ([self isTouchMoving:[touches anyObject]])
{
[self setEnabled:NO];
[self setSelected:NO];
}
}

- (void) restoreAllEnables
{
NSArray *views = [self subviews];

for (UIView *aView in views)
{
if ([aView respondsToSelector:@selector(restoreEnable)])
{
[aView restoreEnable];
}
}
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self restoreAllEnables];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self restoreAllEnables];
}

-(void) restoreEnable
{
NSArray *views = [self subviews];

if ([self respondsToSelector:@selector(enableToRestore)])
{
[self setEnabled:[self enableToRestore]];
}

for (UIView *aView in views)
{
if ([aView respondsToSelector:@selector(restoreEnable)])
{
[aView restoreEnable];
}
}
}



i try to build it n i got a error msg on this line

if (fabsf([self startTouchPosition].x - currentTouchPosition.x) >=2.0)

if i only have the code like this one is builds, but when i click the button is crash,


Code:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == imageView) {
imageView.image = imageDown;
}
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
imageView.image = image;
if ([touch view] == imageView || [touch view] == scrollView) {
imageView.image = image;
}
}


and for the touch even because the touch view is == imageView, so when i have it scrollView is not works, i did try to use touch view == scrollView still not works, how should i fix it?
March 30, 2010 5:52:18 AM PDT (23 weeks ago)
Photo Jonathan Lehr
President
AboutObjects
Member since Mar 12, 2009
Location: South Riding
Posts: 22
You said you got an error message, but you didn't include the error message in your post, and in general you haven't provided much context around the code snippets you posted.

For example, is scrollView an instance variable? Of what? And how does your code handle memory management for it? What is imageDown, and how are you setting it and handling its memory management? What's the data type of startTouchPosition, and is there a reason you couldn't use -[touch previousLocationInView:] instead? Etc.

It's also a little difficult to understand what you're trying to accomplish. For example, you wrote, "also can click do the touch even with the image." Do you mean that touching the button would do the same thing as touching the view? If so, why do you need the subview to be a button at all?

Another note: in general, the code you posted appears to use instance variables directly in a number of places where it could, and arguably should, be using property accessors instead. Using property accessor methods will help you avoid problems with memory management.

-Jonathan
March 30, 2010 7:28:31 AM PDT (23 weeks ago)
Photo Yoyo Zhao
Fontys
Member since Mar 29, 2010
Posts: 3
i had upload the source files maybe will be easy to understand


this is how the image work with touch event look like

http://bbs.in-sl.net/download/WithoutScrollView.zip

this one is after i add the scroll view the touch event for the image is not works

http://bbs.in-sl.net/download/WithScrollView.zip