No touches being triggered! - iphone

OK, I guess my previous question wasn't in detail.
I'm providing more details.
Before posting full source code I have to ask if my question:
My question is Why the following program doesn't call touchesBegan, touchesMoved and touchesEnded? It used to work in previous iOS SDK. My current xcode version is 3.2.5
One comment: The following code dose call all touches if you run it against iPad Simulator. So that's very odd why iPhone simulator and the actual device don't respond to the problem.
main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, #"AppDelegate");
[pool release];
return retVal;
}
AppDelegate.h
#import <UIKit/UIKit.h>
#class GLView;
#interface AppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow* mp_window;
GLView* mp_viewController;
}
#end
AppDelegate.mm
#import "AppDelegate.h"
#import "GLView.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
mp_window = [[UIWindow alloc] initWithFrame: screenBounds];
mp_viewController = [[GLView alloc] initWithFrame: screenBounds];
[mp_window addSubview: mp_viewController];
[mp_window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{}
- (void)applicationDidBecomeActive:(UIApplication *)application
{}
- (void)applicationWillTerminate:(UIApplication *)application
{}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Handle any background procedures not related to animation here.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Handle any foreground procedures not related to animation here.
}
- (void)dealloc
{
[mp_viewController release];
[mp_window release];
[super dealloc];
}
#end
GLView.h
#import <OpenGLES/EAGL.h>
#import <QuartzCore/QuartzCore.h>
#interface GLView : UIView <UIAccelerometerDelegate>
{
#private
EAGLContext* mp_context;
}
- (void) gameLoop;
- (void) drawView: (float) interpolation;
- (void)touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event;
- (void)touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event;
- (void)touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event;
- (void)accelerometer: (UIAccelerometer *) accelerometer
didAccelerate: (UIAcceleration *) acceleration;
#end
GLView.mm
#import "GLView.h"
#import <OpenGLES/ES2/gl.h>
#implementation GLView
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
- (id) initWithFrame: (CGRect) frame
{
if (self = [super initWithFrame: frame])
{
CAEAGLLayer* eaglLayer = (CAEAGLLayer*) super.layer;
eaglLayer.opaque = YES;
EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES1;
mp_context = [[EAGLContext alloc] initWithAPI: api];
if (!mp_context || ![EAGLContext setCurrentContext: mp_context])
{
[self release];
return nil;
}
[mp_context renderbufferStorage: GL_RENDERBUFFER
fromDrawable: eaglLayer];
//TODO: to setup the size of frame for render
//////
//acc
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(0.1f)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
//multi - touch
[self setUserInteractionEnabled:YES];
[self setMultipleTouchEnabled:YES];
[self performSelectorOnMainThread:#selector(gameLoop)
withObject:nil waitUntilDone:NO];
}
return self;
}
- (void) gameLoop
{
while(1)
{
//Get all touches
while(CFRunLoopRunInMode(kCFRunLoopDefaultMode,
0.002f,
TRUE) == kCFRunLoopRunHandledSource);
//render scene
[drawView 1.0f];
}
}
- (void) drawView: (float) interpolation
{
//TODO: adding render image here
//NSLog(#"Render: %f", interpolation);
[mp_context presentRenderbuffer: GL_RENDERBUFFER];
}
- (void) dealloc
{
if ([EAGLContext currentContext] == mp_context)
[EAGLContext setCurrentContext: nil];
[mp_context release];
//TODO: relese all objects here
[super dealloc];
}
- (void)touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
{
int hash;
CGPoint points;
for(UITouch * touch in touches)
{
hash = [touch hash];
points = [touch locationInView: self];
NSLog(#"Touch Began: %d, %f, %f", hash, points.x, points.y);
}
}
- (void)touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event
{
int hash;
CGPoint points;
for(UITouch * touch in touches)
{
hash = [touch hash];
points = [touch locationInView: self];
NSLog(#"Touch Moved: %d, %f, %f", hash, points.x, points.y);
}
}
- (void)touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
{
int hash;
CGPoint points;
for(UITouch * touch in touches)
{
hash = [touch hash];
points = [touch locationInView: self];
NSLog(#"Touch Ended: %d, %f, %f", hash, points.x, points.y);
}
}
- (void) accelerometer: (UIAccelerometer *) accelerometer
didAccelerate: (UIAcceleration *) acceleration
{
NSLog(#"Accelerometer: (%f, %f, %f)",
acceleration.x,
acceleration.y,
acceleration.z );
}
#end
This is only a test case. my main concern is why with new SDK, touches won't be called? Please don't tell me I have to use NSTimer or other timers. I did work before I install the new SDK. I'm just wondering if anyone can see any problem in that source code other than that.
Thanks,
Memphis

I don't know with certainty what your problem is. I do note, however, that you don't have a view controller--while your UIApplicationDelegate subclass has a mp_viewController member, it's a UIView instead of a UIViewController. It is possible that this is the source of your problem.
I suggest adding a UIViewController associated with your view and assigning it to the application delegate's rootViewController property.

Related

How to add a magnifier to custom control?

How to add a magnifier to custom control? Control is a child of UIView. (It's a UIWebView - but native magnification functionality doesn't work at some pages.)
UPDATED:
Maybe it's possible to force a draw of magnifier on UIWebView?
1. Add the following files to your project:
MagnifierView.h:
//
// MagnifierView.h
// SimplerMaskTest
//
#import <UIKit/UIKit.h>
#interface MagnifierView : UIView {
UIView *viewToMagnify;
CGPoint touchPoint;
}
#property (nonatomic, retain) UIView *viewToMagnify;
#property (assign) CGPoint touchPoint;
#end
MagnifierView.m:
//
// MagnifierView.m
// SimplerMaskTest
//
#import "MagnifierView.h"
#import <QuartzCore/QuartzCore.h>
#implementation MagnifierView
#synthesize viewToMagnify;
#dynamic touchPoint;
- (id)initWithFrame:(CGRect)frame {
return [self initWithFrame:frame radius:118];
}
- (id)initWithFrame:(CGRect)frame radius:(int)r {
int radius = r;
if ((self = [super initWithFrame:CGRectMake(0, 0, radius, radius)])) {
//Make the layer circular.
self.layer.cornerRadius = radius / 2;
self.layer.masksToBounds = YES;
}
return self;
}
- (void)setTouchPoint:(CGPoint)pt {
touchPoint = pt;
// whenever touchPoint is set, update the position of the magnifier (to just above what's being magnified)
self.center = CGPointMake(pt.x, pt.y-66);
}
- (CGPoint)getTouchPoint {
return touchPoint;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect bounds = self.bounds;
CGImageRef mask = [UIImage imageNamed: #"loupe-mask#2x.png"].CGImage;
UIImage *glass = [UIImage imageNamed: #"loupe-hi#2x.png"];
CGContextSaveGState(context);
CGContextClipToMask(context, bounds, mask);
CGContextFillRect(context, bounds);
CGContextScaleCTM(context, 1.2, 1.2);
//draw your subject view here
CGContextTranslateCTM(context,1*(self.frame.size.width*0.5),1*(self.frame.size.height*0.5));
//CGContextScaleCTM(context, 1.5, 1.5);
CGContextTranslateCTM(context,-1*(touchPoint.x),-1*(touchPoint.y));
[self.viewToMagnify.layer renderInContext:context];
CGContextRestoreGState(context);
[glass drawInRect: bounds];
}
- (void)dealloc {
[viewToMagnify release];
[super dealloc];
}
#end
TouchReader.h:
//
// TouchReader.h
// SimplerMaskTest
//
#import <UIKit/UIKit.h>
#import "MagnifierView.h"
#interface TouchReader : UIView {
NSTimer *touchTimer;
MagnifierView *loop;
}
#property (nonatomic, retain) NSTimer *touchTimer;
- (void)addLoop;
- (void)handleAction:(id)timerObj;
#end
TouchReader.m:
//
// TouchReader.m
// SimplerMaskTest
//
#import "TouchReader.h"
#import "MagnifierView.h"
#implementation TouchReader
#synthesize touchTimer;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(addLoop) userInfo:nil repeats:NO];
// just create one loop and re-use it.
if (loop == nil) {
loop = [[MagnifierView alloc] init];
loop.viewToMagnify = self;
}
UITouch *touch = [touches anyObject];
loop.touchPoint = [touch locationInView:self];
[loop setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleAction:touches];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.touchTimer invalidate];
self.touchTimer = nil;
[loop removeFromSuperview];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self.touchTimer invalidate];
self.touchTimer = nil;
[loop removeFromSuperview];
}
- (void)addLoop {
// add the loop to the superview. if we add it to the view it magnifies, it'll magnify itself!
[self.superview addSubview:loop];
// here, we could do some nice animation instead of just adding the subview...
}
- (void)handleAction:(id)timerObj {
NSSet *touches = timerObj;
UITouch *touch = [touches anyObject];
loop.touchPoint = [touch locationInView:self];
[loop setNeedsDisplay];
}
- (void)dealloc {
[loop release];
loop = nil;
[super dealloc];
}
#end
Based on: http://coffeeshopped.com/2010/03/a-simpler-magnifying-glass-loupe-view-for-the-iphone
2. Add the following images:
Used images on the code:
loupe-hi#2x.png:
loupe-mask#2x.png:
Original but centered images with a shadow (not used at this moment):
loupe-shadow-hi#2x.png:
loupe-shadow-mask#2x.png:
3. Replace the main UIView on your xib-file by TouchReader
The magnifier will work automaticaly except controls that captures touch events themselfs (for example, UIWebView). And the code above doesn't support the images with a shadow. Please add new answer to the qustion if you successfully fix this issue.
UPDATED:
Change the following code to add UIWebView support. UIView should remain UIView.
#interface TouchReader : UILongPressGestureRecognizer
And add a gesture to webView:
TouchReader* gestureMagnifier = [[[TouchReader alloc] initWithTarget:self action:#selector(handleMagnifier:)] autorelease];
gestureMagnifier.webView = editSource;
gestureMagnifier.delegate = self;
gestureMagnifier.minimumPressDuration = 0.5;
[webView addGestureRecognizer:gestureMagnifier];
TouchReader.h:
//- (void)handleAction:(id)timerObj;
-(void) handleGestureAction:(CGPoint)location;
TouchReader.m:
-(void)awakeFromNib
{
UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[self addGestureRecognizer:longPressGesture];
}
-(void)handleGesture:(UILongPressGestureRecognizer *)longPressGesture
{
CGPoint location = [longPressGesture locationInView:self];
switch (longPressGesture.state) {
case UIGestureRecognizerStateBegan:
self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:#selector(addLoop)
userInfo:nil
repeats:NO];
// just create one loop and re-use it.
if(loop == nil){
loop = [[MagnifierView alloc] init];
loop.viewToMagnify = self;
}
loop.touchPoint = location;
[loop setNeedsDisplay];
break;
case UIGestureRecognizerStateChanged:
[self handleGestureAction:location];
break;
case UIGestureRecognizerStateEnded:
[self.touchTimer invalidate];
self.touchTimer = nil;
[loop removeFromSuperview];
loop=nil;
break;
default:
break;
}
}
- (void)addLoop {
// add the loop to the superview. if we add it to the view it magnifies, it'll magnify itself!
[self.superview addSubview:loop];
}
-(void) handleGestureAction:(CGPoint)location
{
loop.touchPoint = location;
[loop setNeedsDisplay];
}
You don't need the touches... methods any more.

UIImage can not detect touch

I used codes below to detect the touch on UIImage
//
#import <UIKit/UIKit.h>
#interface KUIImageView : UIImageView
{
CGPoint startLocation;
}
#end
#implementation KUIImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (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
I set the breakpoint at touchesBegan
But it was not triggered
the UIImage was on a UIScrollView by using
[aScrollView addObject:aUIImageView];
Welcome any comment
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// here
self.userInteractionEnabled = YES;
}
return self;
}

iphone opengl es 2.0 custom build a gl view failed,but did not know why

I want to build a opengl es 2.0 view extend from UIview.
And flow this article:http://www.raywenderlich.com/3664/opengl-es-2-0-for-iphone-tutorial
when I finished first step. everything runs wrong.
It did not run as I throught.
Here is my code:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
#interface NXGLES2View : UIView
- (void) render;
#end
#import "NXGLES2View.h"
#interface NXGLES2View ()
{
CAEAGLLayer *eaglLayer_;
EAGLContext *glContext_;
GLuint colorRenderBuffer_,frameBuffer_, depthRenderBuffer_;
CADisplayLink *displayLink_;
}
- (void) setupGL_;
- (void) tearDownGL_;
- (void) setupLayer_;
- (void) setupContext_;
- (void) setupRenderBuffer_;
- (void) setupFrameBuffer_;
- (void) setupDepthBuffer_;
- (void) setupDisplayLink_;
#end
#implementation NXGLES2View
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setupGL_];
}
return self;
}
#pragma mark -
#pragma mark setup step
+ (Class)layerClass{
return [CAEAGLLayer class];
}
- (void) setupLayer_{
eaglLayer_ = (CAEAGLLayer *)self.layer;
eaglLayer_.opaque = YES;
}
- (void) setupContext_{
glContext_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!glContext_) {
NSLog(#"failed to create eagl context,abort");
abort();
}
if (![EAGLContext setCurrentContext:glContext_]) {
NSLog(#"failed to set gl context, abort");
abort();
}
}
- (void) setupRenderBuffer_{
glGenRenderbuffers(1, &colorRenderBuffer_);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderBuffer_);
[glContext_ renderbufferStorage:GL_RENDERBUFFER fromDrawable:eaglLayer_];
}
- (void) setupDepthBuffer_{
glGenRenderbuffers(1, &depthRenderBuffer_);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderBuffer_);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, self.frame.size.width, self.frame.size.height);
}
- (void) setupFrameBuffer_{
glGenFramebuffers(1, &frameBuffer_);
glBindRenderbuffer(GL_FRAMEBUFFER, frameBuffer_);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderBuffer_);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer_);
}
- (void) setupDisplayLink_{
displayLink_ = [CADisplayLink displayLinkWithTarget:self selector:#selector(render)];
[displayLink_ addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void) setupGL_{
[self setupLayer_];
[self setupContext_];
[self setupDepthBuffer_];
[self setupRenderBuffer_];
[self setupFrameBuffer_];
}
- (void) tearDownGL_{
glDeleteRenderbuffers(1, &colorRenderBuffer_);
glDeleteRenderbuffers(1, &depthRenderBuffer_);
glDeleteFramebuffers(1, &frameBuffer_);
}
#ifndef __IPHONE_5_0
- (void) dealloc{
[self tearDownGL_];
[glContext_ release];
glContext_ = nil;
[super dealloc];
}
#endif
#pragma mark -
#pragma mark draw
- (void) render{
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
[glContext_ presentRenderbuffer:GL_RENDERBUFFER];
}
then I create empty project and do as this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
NXGLES2View *glview = [[NXGLES2View alloc] initWithFrame:self.window.bounds];
[self.window addSubview:glview];
[glview performSelector:#selector(setupDisplayLink_) withObject:nil afterDelay:2.0];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
When app run. it did not displayed a
glClearColor(1.0, 0.0, 0.0, 1.0);
red color.
and I'v check the article again and again.I can not figure out where is wrong.
help me please.
thanks again.
I found I made a mistake in setupFrameBuffer_ method.
glBindRenderbuffer(GL_FRAMEBUFFER, frameBuffer_);
this should be :
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer_);

Schedule Update from Another Layer

I have an sprite the I need to rotate it with touch but it is located in a different layer. Is it possible to update it's position?
E.G.
Sprite has it's own layer but it's position needs to be updated within the main gamescene
here is what I have so far.
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "GameScene.h"
#interface G : CCLayer {
CCSprite *g;
CGFloat gRotation;
}
#end
------------------------------------------
#import "G.h"
#implementation G
-(id) init
{
if ((self = [super init]))
{
CCLOG(#"%#: %#", NSStringFromSelector(_cmd), self);
g = [CCSprite spriteWithFile:#"g.png"];
[self addChild:g z:-1];
//[self scheduleUpdate];
if (g.rotation == 360)
{
[self unscheduleUpdate];
}
}
return self;
}
- (void)update:(ccTime)delta
{
g.rotation = gRotation;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, g.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, g.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
gRotation += currentTouch - previousTouch;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void) dealloc
{
CCLOG(#"%#: %#", NSStringFromSelector(_cmd), self);
[super dealloc];
}
#end
GameScene
#import "GameScene.h"
#import "MainMenu.h"
#import "G.h"
#implementation GameScene
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
GameScene *layer = [GameScene node];
[scene addChild: layer];
return scene;
}
-(void) tapG: (id) sender
{
G *gView;
gView = [[G alloc] init];
gView.position = ccp(100, 100);
[self.parent addChild:gView z:1001];
[gView schedule:#selector(update:)];
[gView release];
}
-(id) init
{
if ((self = [super init]))
{
tG = [CCMenuItemImage itemFromNormalImage:#"tp.png" selectedImage:#"tp.png" disabledImage:#"tpaperd.png" target:self selector:#selector(tapG:)];
gt = [CCMenu menuWithItems:tG, nil];
gt.position = ccp(210, 80);
[gt alignItemsHorizontallyWithPadding:10];
[self addChild:gt z:0];
}
return self;
}
- (void) dealloc
{
CCLOG(#"%#: %#", NSStringFromSelector(_cmd), self);
[super dealloc];
}
It brings up the sprite but the sprite does not rotate.
you can schedule updates for another layer.
are you sure your G layer is set to be touch enabled? try gView.isTouchEnabled = YES; after allocating gView. you may need to confirm to the protocol. Add <CCStandardTouchDelegate> to your CCLayer interface: #interface G : CCLayer <CCStandardTouchDelegate> { ....
also you can schedule -(void)update:(ccTime)delta method using [gView scheduleUpdate]; it will give the same result but its more convenient.

how to animate image on shake effect?

I have implemented game application.In which i want to animate some area of images on shake effect.How it possible please give me advice.
Edit:
My excatly query is that:
In my game application there is one image.Now i am selected some area frame of image.now i am shaking iphone then only selected area of frame image must be animate.
Here is a skeleton ... then you can get more detail at the suggested docs. This is real simple-minded but has a randomization and you can instantiate with your own image. I created an image container as a UIViewController and in my main (either your AppDelegate or the RootViewController) you create the instance in viewDidLoad. The methods that have to be overloaded in your AppDelegate, (the code is at the end of the post) so it receives shakes, are:
viewWillAppear
viewDidAppear
viewWillDisappear
viewDidDisappear
canBecomeFirstResponder
becomeFirstResponder
nextResponder
You become a responder in viewWillAppear and you have to enable the device notifications (verified that there is a documented bug) so the motion methods get invoked.
motionBegan:withEvent
motionEnded:withEvent
I like to put an NSLog(#"%s", FUNCTION); statement in all my first-time written methods, that way I can quickly determine if something is missing to get my events, properly initialized, etc. Its just my style.
AnimatedImage.h isA ViewController
#interface AnimatedImage : UIViewController {
UIImageView *image;
UILabel *label;
float cx;
float cy;
float duration;
float repeat;
}
#property (nonatomic, retain) UIImageView *image;
#property (nonatomic, retain) UILabel *label;
-(id) initWithImageName: (NSString*) imageName;
-(IBAction) shake;
AnimatedImage.m
#include <stdlib.h>
#define random() (arc4random() % ((unsigned)RAND_MAX + 1))
#import "AnimatedImage.h"
#implementation AnimatedImage
#synthesize image;
#synthesize label;
-(id) initWithImageName: (NSString*) imageName {
NSLog(#"%s", __FUNCTION__);
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
return self;
}
-(IBAction) shake {
cx = 0.90 + (random() % 10) / 100; // cx = 0.95;
cy = 0.90 + (random() % 10) / 100; // cy = 0.95;
duration = ( 5.0 + random() % 10) / 1000.0;
repeat = (65.0 + random() % 20);
image.transform = CGAffineTransformScale(CGAffineTransformIdentity, cx, cy);
[UIView beginAnimations: #"identifier" context: #"shake"];
[UIView setAnimationDelegate:image.superclass];
[UIView setAnimationDuration: duration]; // 0.008];
[UIView setAnimationRepeatCount: repeat]; // 85.0];
[UIView setAnimationRepeatAutoreverses: YES];
image.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
[UIView commitAnimations];
}
-(IBAction) bounce {
image.transform = CGAffineTransformTranslate(image.transform, -20, -6);
[UIView beginAnimations: #"identifier" context: #"bounce"];
[UIView setAnimationDelegate:image.superclass];
[UIView setAnimationDuration: duration];
[UIView setAnimationRepeatCount: repeat];
[UIView setAnimationRepeatAutoreverses: YES];
image.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
[UIView commitAnimations];
}
#end
This "root or main" delegate is a UIViewController. I have left of detail to show its simplicity, but still left my 'trace' code that I find essential for first-time debugging. Also, as an aside, I believe you have to overload motionBegan, even if you only need the motionEnded event. I recall that my app did not work and then I added the motionBegan and it started to call motionEnded. It kinda' makes sense that it would be that way, but I don't have any documentation to back it up. A simple experiment after you get it working can confirm or deny my comment.
- (void)viewDidLoad {
[super viewDidLoad];
[super becomeFirstResponder];
.
.
.
NSLog(#"%s", __FUNCTION__);
NSLog(#"%s: I %s first responder! (%#)", __FUNCTION__, [self isFirstResponder] ? "am" : "am not", self);
.
.<created image in this ViewController's NIB using IB>
.
someImage = (UIImageView *) [self.view viewWithTag:TAG_SOMEIMAGE];
aniImage = [[AnimatedImage alloc] init];
aniImage.image = someImage;
}
-(void) viewWillAppear: (BOOL) animated{
NSLog(#"%s", __FUNCTION__);
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(receivedRotate:)
name: UIDeviceOrientationDidChangeNotification
object: nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"%s", __FUNCTION__);
[super becomeFirstResponder];
assert( [self canPerformAction:#selector(motionEnded:withEvent:) withSender:self] );
}
- (void)viewWillDisappear:(BOOL)animated {
NSLog(#"%s", __FUNCTION__);
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver: self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(#"%s", __FUNCTION__);
[super resignFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
NSLog(#"%s", __FUNCTION__);
return YES;
}
- (BOOL)becomeFirstResponder {
NSLog(#"%s", __FUNCTION__);
return YES;
}
- (UIResponder *)nextResponder {
NSLog(#"%s", __FUNCTION__);
return [self.view superview];
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(#"%s", __FUNCTION__);
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(#"%s", __FUNCTION__);
if( [self isFirstResponder] ) {
[aniImage shake];
}
}
This code does work -- but if I snipped out too much detail, just ask and I will make certain to add enough to help you along. This was a really rewarding task for me and I am excited to share it with someone looking for some help in the same experience.
Have a look at LocateMe example code, one of the first samples in the SDK and use and change the bounce back to centre code.