How to move an image - xcode

I am creating an app where bombs come into the screen and blow up this thing that you have to move. How do I make these bombs fly into the screen and move in random directions automatically ? Kind of like fruit ninja where the fruits fly out at you. How do I code that ? What do I need to do ? Please also provide some code. Thank you !

Create a class, called Bomb or something and it needs to have an instance variable and property of CGRect on iOS or NSRect on Mac OS.
Then create a method that can draw the bomb on the screen, like - drawOnScreen or just - draw which knows how to draw the object. Which would contain code like [[UIImage imageNamed:#"bomb1"] drawInRect:self.frame]
Then in your game loop you simple change the coordinates of the frame of such a bomb object to make it move, of course you will need to do that before calling the draw method as it would draw the previous frame then..
There also is another way of adding a subview to the gameview every time you need a new bomb. However a view has more overhead than using the object I described above and would most likely cause your game to run slow if you have like 50/60+ ( wild guess ) bombs.
For controls on the screen like a pause button I would definitely use a view and add it as a subview.

Related

How to update a subclassed UIView for drawing when the slider is changed?

Some time ago I saw this on one of the iTunes U movies (I think it was stanford).
I can't find the exact video unfortunately... So if you know please tell me.
I want to create a view where the user can use the slider to change the smiley face.
So my current plan is to subclass UIView, then use drawInRect to make the drawing.
But how do I hook the slider value to my drawing and how do I update it 'smoothly'?
In your value changed handler for the slider, ask the view to redraw itself. The view should clear its context first, then do whatever it needs to do given the inputs you'll pass to it (the value of the slider) before asking it to redraw itself using -setNeedsDisplay et al.

Level Metering UIView

How could I move a subview to the front of the stack when the LevelMeter detects a certain amount of decibels? I want LevelMetering to be active in real time without a record play function.
Ultimately, I'm trying to animate a mouth to open and close based on sound.
To make a view topmost in the view order, you just have to send a -bringSubviewToFront: to the view that contains it. From a view controller, you might do something like:
[self.view bringSubviewToFront:levelMeterView];
If that doesn't answer your question, perhaps you can expand a bit on what you're asking?

NSBezierPath point animation in NSView

I've built a custom control in Cocoa which is drawn using an NSBezierPath and I'd like it to change shape when it's state has changed (unused state = a pointed 'look here' edge, used state = standard control edge).
It feels like I've looked through every mention of "NSBezierPath" and "Animation" there is on the web but with no luck.
Before I crack out some NSTimers and write my own timing & path point controls, does anyone know if this is possible using Core Animation or similar?
You could use NSAnimation. Create your own NSAnimation subclass, and override setCurrentProgress method.
In that method, you can change your NSBezierPath size, shape or whatever you need, based on current animation progress. After that you can force a redraw in the view (via delegate, for instance) to re-display the path.

Changing selection state of IKImageBrowserView

Hey guys, I've just migrated my image selector from NSCollectionView to IKImageBrowserView. I've got almost everything set up the way I want it, except for the selection ring. I don't like the greyed out background that IKImageBrowserView defaults to, and I wanted to do a yellow stroke around the edge of my selected image to indicate it's selection (like in iPhoto). Is is possible to override the draw state of IKImageBrowserCell? I haven't been able to find any way to do it yet. It doesn't have the simple drawRect methods that I'm used to. Any help would be appreciated. I'm assuming I have to use CALayers?
I overrode - (CALayer *)layerForType:(NSString *)type and tried just as a test, setting the layer corner radius to 0, but it didn't seem to change anything. The method is being called because if I throw a breakpoint in it, it stops there. However, even if I return nil from that method, it still draws the images like usual.
Thanks!
That is the right method for customizing the IKImageBrowserCell.
Using CALayers and configuring different attributes, you can control many facets of how the images are presented.,
A layer of type = IKImageBrowserCellSelectionLayer is what you will want to change to have the display behave and present as you wish.
Here's a link to Apple's sample code project that will get you started

Creating a view with draggable text elements

I am trying to create a view for a kind of brainstorming application like, for example, OmniGraffle, with elements that contain textviews and can be dragged around. (Also, the should be connectable with arrows, but that is not (yet) the problem)
I did my homework and searched via google and read books about cocoa, but there seems to be no similar example around.
Since I am also new to cocoa, I’m a bit helpless here.
The thing I am sure of is, that I need a custom view in which I can create my elements - what I tried until now to do that is:
First, I searched for the syntax to add subwindows to a window to create my elements. Subwindows, I imagined, would automatically be movable and come to front and so on.
The problem: As the experienced Cocoa-programmers of you probably are not surprised, I was stunned to find nothing about anything like that - this seems to be something, that is just not intended in Cocoa?!
Then I thought about creating subviews that contain a custom view for the title bar drawing (where the user can click to drag the element) and a NSTextView.
Problems:
I read, that it is not so clever to create dozens of subviews in a window because that would be very slow (or would that be not so bad in this case because all the subviews would be instances of always the same class?).
Also I can’t find out how to load a subview from a nib- or xib-file. Would I need a viewController? Or would that make the dozens-of-instances-problem even worse?
And Apple tells you not to overlap subviews (okay, that would be not so important, but I really wonder how the guys at OmniGroup made OmniGraffle...)
Because of that, I now wanted to do the title-bar-drawing in the surrounding custom view and create the textview programmatically (as I understand, a text-“view“ ist not really a view and takes its functionality from NSCell to reduce all the effort with the views?).
Problems:
Even that failed because I was not able to create a textview that doesn’t fill the complete window (the initWithFrame: of the [[NSScrollView alloc] initWithFrame: aRect] just seems to be ignored or do I get that wrong?).
Also, there should be some buttons on each element in the final application. I imagine that would be easier to accomplish with a subview from a nib-file for each element?
Well, now that nothing works and the more I read, the more problems seem to occur, I am pretty confused and frustrated.
How could I realize such a program? Could someone please push me in the right direction?
I created a class for the draggable elements where I save position, size and text in instance variables. In my view, every new element instance is added to an array (for now, this works without a controller). The array is used to draw all the elements in a loop in drawRect:. For the text of the element I just use a NSTextFieldCell which is set to the saved text from every element in the same loop.
That way it is also possible to overlap the elements.

Resources