Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-18-2010, 06:54 PM   #1 (permalink)
jcc
Registered Member
 
Join Date: Aug 2010
Posts: 53
jcc is on a distinguished road
Default Spawn and Drag UIImage

I am spawning and dragging a UIImageView however, when it comes time to set the image in it's new position the app crashes.

Am I missing something?

Draggable.h:
Code:
#import <Foundation/Foundation.h>
@interface Draggable : UIImageView {
    CGPoint startLocation;
}
@end


Draggable.m:
Code:
#import "Draggable.h"
@implementation Draggable

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    // Retrieve the touch point
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
    [[self superview] bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    // Move relative to the original touch point
    CGPoint pt = [[touches anyObject] locationInView:self];
    CGRect frame = [self frame];
    frame.origin.x += pt.x - startLocation.x;
    frame.origin.y += pt.y - startLocation.y;
    [self setFrame:frame];
}
@end


Call it:
Code:
	UIImage *tmpImage = [[UIImage imageNamed:@"myImage.png"] retain];
	UIImageView *imageViewX = [[UIImageView alloc] initWithImage:tmpImage];
	
	CGRect cellRectangle;
	cellRectangle = CGRectMake(0,0,tmpImage.size.width ,tmpImage.size.height );
	UIImageView *dragger = [[Draggable alloc] initWithFrame:cellRectangle];
	[dragger setImage:tmpImage];
	[dragger setUserInteractionEnabled:YES];
	
	[self.view addSubview:dragger];
	[self.view bringSubviewToFront:dragger];
	[imageViewX release];
	[tmpImage release];
jcc is offline   Reply With Quote
Old 08-18-2010, 07:02 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

It might be useful to post the contents of the console when the app crashes, but I imagine it has something to do with this line:
Code:
UIImageView *dragger = [[Draggable alloc] initWithFrame:cellRectangle];
I think it should be
Code:
Draggable *dragger = [[Draggable alloc] initWithFrame:cellRectangle];
And make sure you release it when you're done, addSubview: will retain it for you.
harrytheshark is offline   Reply With Quote
Old 08-18-2010, 09:20 PM   #3 (permalink)
jcc
Registered Member
 
Join Date: Aug 2010
Posts: 53
jcc is on a distinguished road
Default

Thanks for your help. I tried making the changes you suggested but nothing has changed. The console shows the following . . . although, I don't know what it means.

2010-08-18 19:18:30.989 Book-iPad[7021:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil'
2010-08-18 19:18:30.990 myApp[7021:207] Stack: (
46651472,
47809324,
46389003,
46388842,
1006603,
447296,
447060,
10272,
4007787,
3399144,
3290691,
3322328,
55292284,
45930652,
45926568,
55285917,
55286114,
3314546,
8802,
8674
)
terminate called after throwing an instance of 'NSException'
jcc is offline   Reply With Quote
Old 08-19-2010, 03:31 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

It looks like "dragger" is returning nil. You can check by doing this:
Code:
if (dragger == nil) {
    NSLog(@"Yup");
}
harrytheshark is offline   Reply With Quote
Old 08-19-2010, 11:29 AM   #5 (permalink)
jcc
Registered Member
 
Join Date: Aug 2010
Posts: 53
jcc is on a distinguished road
Default

Quote:
Originally Posted by harrytheshark View Post
It looks like "dragger" is returning nil. You can check by doing this:
Code:
if (dragger == nil) {
    NSLog(@"Yup");
}

Thanks for the help. Unfortunately, the console doesn't return "Yup", so I don't think the dragger is returning nil. Any other ideas?
jcc is offline   Reply With Quote
Old 08-19-2010, 12:14 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

Puzzling, are you using an array at any point?
harrytheshark is offline   Reply With Quote
Reply

Bookmarks

Tags
drag, spawn, touch, uiimageview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 345
5 members and 340 guests
freewind, givensur, lendo, Newbie123, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:53 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0