Add MenuItem Images In top of the imageview in cocos2d - iphone

In my Layer i have added One image view then i want to add CCMenuItem Images on top of the Image View.
How can i do this in Cocos2d
give me any suggestions
edit : moved code from comments
img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 35, 320, 480)];
img.backgroundColor=[UIColor clearColor];
[[[CCDirector sharedDirector] openGLView] addSubview: img];
CCMenuItemImage *topBarBtn1 = [CCMenuItemImage itemFromNormalImage:#"TapButtonR.png"
selectedImage:#"TapButtonR.png"
target:self
selector:#selector(gamePlay:)];
CCMenu *tapScreen = [CCMenu menuWithItems:topBarBtn1, nil];
tapScreen.position = ccp(160, 70);
[self addChild:tapScreen z:2 ];

Related

The zoom for uiscrollview isn't working

I don't understand why the scrollview isn't working, because I followed advices from tutorials.
I created the UIScrollView that contains an UIImageView and I also added the method
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imgv;
}
and the UIScrollViewDelegate in the interface, but it isn't working.
Here is the code for the UIScrollView:
imgv = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 0, 1000, 500)];
imgv.image = [UIImage imageWithData:retrievedData];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 800, 800)];
scrollView setContentSize:CGSizeMake(imgv.image.size.width, imgv.image.size.height)];
scrollView.maximumZoomScale = 4;
scrollView.minimumZoomScale = 1;
[scrollView setScrollEnabled:YES];
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(hideImageView)];
[scrollView addSubview:imgv];
[view addSubview:scrollView];
I would be thankful, if someone could figure out what the problem could be.
You need to return imgv in your delegate method viewForZoomingInScrollView::
- (UIView *) viewForZoomingInScrollView:(UIScrollView *) view {
return imgv;
}
You have to add the imageView as a subview to the scrollview
[scrollView addSubview:imgv];
I believe you are forgetting to set the frame of your imageView properly
self.imageView.frame=CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);

UIScrollView with UITapGestureRecognizer and UIPageControl

I'm creating a horizontal scrolling tableview containing images. I have the swiping functionality working great, and am able to add the images to the scrollView as so in cellForRowAtIndexPath:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
[scrollView setContentSize:CGSizeMake(700, 78)];
UIImage *footImage = [UIImage imageNamed:#"Foot.png"];
UIImageView *footImageView = [[UIImageView alloc] initWithImage:footImage];
footImageView.frame = CGRectMake(0, 0, 80, 78);
footImageView.userInteractionEnabled = YES;
[scrollView addSubview: footImageView];
UIImage *handImage = [UIImage imageNamed:#"Hand.png"];
UIImageView *handImageView = [[UIImageView alloc] initWithImage:handImage];
handImageView.frame = CGRectMake(90, 0, 80, 78);
handImageView.userInteractionEnabled = YES;
[scrollView addSubview: handImageView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(highlightImage)];
[scrollView addGestureRecognizer:tapGesture];
NSLog(#"tapGesture added to scrollView");
[[cell contentView] addSubview:scrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, tv.frame.size.width, 50)];
[pageControl setNumberOfPages:4];
[[cell contentView] addSubview:pageControl];
My tapGesture is registering with the selector, hightlightImage, and just logging that its calling this method correctly.
- (void) highlightImage
{
NSLog(#"tapping tapping");
}
What I REALLY am working for is the ability to highlight/select these images if tapped, and highlight/deselect them if tapped again. I will have another button on screen that will navigate to the next page (irrelevant here).
Am I going down the right path here? Obviously I will populate an NSArray of UIImages and populate the scrollView that way, but just spiking it out for now. If someone could give me some direction or an example of how to make each button separately selectable/de-selectable, that would be GREAT:)
Use UIButton instead of UIImageView.
It's got built-in selected functionality.
get rid of your gestures and add highlightImage as the action to every button.
make your highlightImage into highlightImage:(id)sender
sender should be a button so you can just do
[sender setSelected:YES];

uiimage view and rectangle

I want to draw a rectangle on one image
how to do that?
Start by reading Quartz 2D Programming Guide and go to paths section.
-(IBAction)Handeltap:(UIGestureRecognizer *)sender
{
if(sender.state==UIGestureRecognizerStateBegan)
{
tapPoint1 = [sender locationInView:sender.view];
NSLog(#"UIGestureRecognizerStateBegan and x=%d and y=%d",(int)tapPoint1.x,(int)tapPoint1.y);
img1=[[UIImageView alloc]initWithImage:nil];
[img1 setBackgroundColor:[UIColor lightTextColor]];
CGRect rect1=CGRectMake((float)tapPoint1.x,(float)tapPoint1.y,50,20);
NSLog(#"rect=%f and %f and %f and %f",rect1.origin.x,rect1.origin.y,rect1.size.width,rect1.size.height);
[img1 setFrame:rect1];
[self.view addSubview:img1];
[self.view bringSubviewToFront:img1];
}
if(sender.state==UIGestureRecognizerStateChanged)
{tapPoint2 = [sender locationInView:sender.view];
// NSLog(#"UIGestureRecognizerStateChanged and x=%d and y=%d",(int)tapPoint.x,(int)tapPoint.y);
[img1 setFrame:CGRectMake(img1.frame.origin.x,img1.frame.origin.y,(float)tapPoint2.x-img1.frame.origin.x,(float)tapPoint2.y-img1.frame.origin.y)];
}
if(sender.state==UIGestureRecognizerStateEnded)
{
tapPoint2 = [sender locationInView:sender.view];
NSLog(#"UIGestureRecognizerStateEnded and x=%d and y=%d",(int)tapPoint2.x,(int)tapPoint2.y);
[img1 setFrame:CGRectMake(img1.frame.origin.x,img1.frame.origin.y,(float)tapPoint2.x-img1.frame.origin.x,(float)tapPoint2.y-img1.frame.origin.y)];
}
}
add tap gesture and paste this
UILongPressGestureRecognizer *ges11=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(Handeltap:)];
[self.view addGestureRecognizer:ges11];
Tanks for helping me..
If you just want to add a simple colored rectangle to an image you can do it like this:
UIImage *imageFile = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"myImageFile" ofType:#"jpg"]];
UIImageView *myImageFileView = [[UIImageView alloc] initWithImage:imageFile];
myImageFileView.frame = CGRectMake(0, 0, 320, 480); //size of image view
[self.view addsubview:myImageFileView]; //I assume this is within a view controller .m
UIView *rectangle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; //A rectangle at point (0, 0) 50x50 in size.
rectangle.backgroundColor = [UIColor redColor]; //color the rectangle
[myImage addSubview:rectangle]; //add the rectangle to your image
[rectangle release];
[imageFile release];
[myImageFileView release];

How to zoom particular part of UIScrollView?

HI,
I develop an application in which I want to implement the splash screen, on that splash screen I want to bind the scrollView and UIImage. My code as follow,
-(void)splashAnimation{
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
//scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView = [[UIScrollView alloc] initWithFrame:[window bounds]];
scrollView.pagingEnabled = NO;
scrollView.bounces = NO;
UIImage *image = [UIImage imageNamed:#"splash.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = NO;
[scrollView addSubview:imageView];
[scrollView setDelegate:self];
//[scrollView release];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self splashAnimation];
[self initControllers];
[window addSubview:[mainTabBarController view]];
[window makeKeyAndVisible];
}
On my given code the one blank window comes up and stay on.
I want to on that blank screen bind my splash.png.
****The Above problem is solved****
My current code is
scrollView.pagingEnabled = NO;
scrollView.bounces = NO;
UIImage *image = [UIImage imageNamed:#"splash.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = NO;
[scrollView addSubview:imageView];
scrollView.maximumZoomScale = 4.0f;
scrollView.minimumZoomScale = 1.0f;
CGRect rect = CGRectMake(119, 42, 208, 166);
[scrollView zoomToRect:rect animated:YES];
[scrollView setDelegate:self];
[window addSubview:scrollView];
[window makeKeyAndVisible];
I want to zoom the particular part of scrollView.
RajB - for your zooming of the scrollView do this:
CGRect zoomRect = CGRectMake(119, 42, 208, 166);
[scrollView zoomToRect:zoomRect animated:YES];
Hope this helps!
You create a UIScrollView but never add it to the view hierarchy, therefore it will never get displayed. Call [window addSubview:scrollView], and then don't forget to release it.
If you are using a MainWindow.xib in your project, your window is created for you, you do not need to create your own.
Use [[UIScreen mainScreen] instead of CGRect(0, 0, 320, 420) <- I also believe you meant "480"
After setting up your splash animation, you call [window addSubview:[mainTabBarController view]]. Even after adding your scrollview as previously mentioned, this will then become the topmost and therefore visible view.
Delay the [window addSubview:[mainTabBarController view]] until after your splash animation completes.

Adding a UIButton to UIScrollView

I am trying to add the UIButton to UIScrollView.
I have added the UIImageView with background image *(size: 320 * 620)*.
Then I added this UIImageView to UIScrollView, and It works fine.
But now I want to add UIButton at position at : (60, 500); (below screen that would appear after scrolling).
I have tried following code, but the button is added on UIView not on scrollview. Button is not getting displayed on the top.
Code :
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = TRUE;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SingleModelVar.png"]];
self.imageView = tempImageView;
[tempImageView release];
imageView.userInteractionEnabled = YES;
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; //fit to screen
//scrollView.delaysContentTouches = NO;
scrollView.contentSize = CGSizeMake(320, imageView.frame.size.height); //imageView.frame.size.height=620 here
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
//The Problem begins ..........
btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];
[scrollView addSubview:btnStartDate];
//[self.view addSubview:btnStartDate];
btnEndDate=[[UIButton alloc] initWithFrame:CGRectMake(60,530,100,20)];
[scrollView addSubview:btnEndDate];
//[self.view addSubview:btnEndDate];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
}
it is not displayed at the top because you added them before you added the imageView.
Put [scrollView addSubview:imageView]; before btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];