Improper iPhone orientation? - iphone

I can't figure out why this isn't working. I'm writing a game that involves scrolling rows/columns of blocks around the screen. I have my orientation limited strictly to Portrait, but it doesn't behave as so.
When I call the method below, it is handed the velocity from the pan. Using logs I was able to see that holding my device upside-down and panning around acts as if my orientation supports Portrait. How can I -completely- turn off anything but Portrait?
Here's some code to help explain what I want to do, and to hopefully justify my sanity.
bool horizontalPan = (fabs(velocity.x) >= (fabs(velocity.y)));
if (horizontalPan)
{
if (fabs(velocity.x) > MAX_VELOCITY)
{
if (velocity.x > 0)
velocity = CGPointMake(MAX_VELOCITY, 0);
else
velocity = CGPointMake(-MAX_VELOCITY, 0);
}
else
{
velocity = CGPointMake(velocity.x, 0);
}
velocity = ccpMult(velocity, PAN_SENSITIVITY);
[self panRow:velocity object:object];
}
else
{
if (fabs(velocity.y) > MAX_VELOCITY)
{
if (velocity.y > 0)
velocity = CGPointMake(0, MAX_VELOCITY);
else
velocity = CGPointMake(0, -MAX_VELOCITY);
}
else
{
velocity = CGPointMake(0, velocity.y);
}
velocity = ccpMult(velocity, PAN_SENSITIVITY);
[self panColumn:velocity object:object];
}
}

How did you 'strictly limit to portrait' did you use
-(BOOL)shouldAutorotate{
return NO;
}

Related

video rotation using AVMutableVideoCompositionLayerInstruction

I am merging multiple videos and I want to detect which ones are in portrait mode and rotate them in landscape so that all movies are in landscape mode... I have done everything and works perfectly except the actual rotate, I guess it's something with the center of the rotation or a composed rotation.
AVMutableVideoCompositionLayerInstruction *videoTrackLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
if([[AppDelegate sharedInstance] orientationForTrack:avAsset] == UIDeviceOrientationPortrait)
{
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI/2);
//CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation(640, 480);
//CGAffineTransform mixedTransform = CGAffineTransformConcat(rotation, translateToCenter);
[videoTrackLayerInstruction setTransform:rotation atTime:nextClipStartTime];
}
else if([[AppDelegate sharedInstance] orientationForTrack:avAsset] == UIDeviceOrientationPortraitUpsideDown)
{
CGAffineTransform rotation = CGAffineTransformMakeRotation(-M_PI/2);
[videoTrackLayerInstruction setTransform:rotation atTime:nextClipStartTime];
}
else if([[AppDelegate sharedInstance] orientationForTrack:avAsset] == UIDeviceOrientationLandscapeLeft)
{
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI);
[videoTrackLayerInstruction setTransform:rotation atTime:nextClipStartTime];
}
else if([[AppDelegate sharedInstance] orientationForTrack:avAsset] == UIDeviceOrientationLandscapeRight)
{
[videoTrackLayerInstruction setTransform:CGAffineTransformIdentity atTime:nextClipStartTime];
}
How can I rotate them properly? I have tried multiple sources but nothing rotates them as they should. I am not interested in the 320 scale fit solution I want the video to keep as much resolution as possible before exporting using AVAssetExportSession.
A solution like this:
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(degreesToRadians(90.0));
CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotateTransform,320,0);
won't suit my needs as I have tried it. Do you have any ideas? Some help will be pretty much appreciated.

Implementing zoom in\out using UIImagePickerController

I have been using UIImagePickerController for my application preview purpose, I have customized and zoom in/out buttons, I didn't used CGAffineTransformScale before, however I have googled and implemented zoom in functionality using CGAffineTransformScale like this...
- (void) zoom_in_clkd
{
preview_picker.cameraViewTransform = CGAffineTransformScale(preview_picker.cameraViewTransform, 1.0, 1.2499);
}
It seems fine, however I want to implement zoom out functionality, I don't have any idea what should be its sx,sy values in CGAffineTransformScale.
Can anyone please tell a good tutorial or sample code to use CGAffineTransformScale?
sx and sy suggests the zoom factor on x and y values respectively.
If you want zoom out the view provide the sx and sy < 1.0
Download the sample code from this link.
I have simulated zoom out functionality in different way, What I have done is I reset to identity(bringing back to original size) and zooming in again to what position I want like this..
if (cameraTransformX == 2.0) {
preview_picker.cameraViewTransform = CGAffineTransformIdentity;
cameraTransformX -= 1.0;
} else if (cameraTransformX == 3.0) {
preview_picker.cameraViewTransform = CGAffineTransformIdentity;
preview_picker.cameraViewTransform = CGAffineTransformScale(preview_picker.cameraViewTransform, 1.0, 1.2499);
cameraTransformX -= 1.0;
} else if (cameraTransformX == 4.0) {
preview_picker.cameraViewTransform = CGAffineTransformIdentity;
preview_picker.cameraViewTransform = CGAffineTransformScale(preview_picker.cameraViewTransform, 1.0, 1.2499);
preview_picker.cameraViewTransform = CGAffineTransformScale(preview_picker.cameraViewTransform, 1.0, 1.2499);
cameraTransformX -= 1.0;
}
I know its not a proper way to go back to previous transform, but it will do the tricks. :)
My solution was to save initial cameraViewTransform and use that for both zoom in/zoom out. For zoom out you reduce scale, set initially for example at 3. Here is my code:
// initial cameraViewTransform
self.initialTransform = self.videoRecorderController.cameraViewTransform;
// for zoom in
if(self.zoomIn) {
// increment scale
self.scale += 0.5;
self.videoRecorderController.cameraViewTransform = CGAffineTransformScale(self.initialTransform, self.scale, self.scale);
}
// for zoom out
else {
self.scale -= 0.5;
self.videoRecorderController.cameraViewTransform = CGAffineTransformScale(self.initialTransform, self.scale , self.scale);
}

creating transperent button in cocos2d iphone

I am developing a cocos2d game. In that game I hace a character, and according to the touch positions on the caracter different actions should be triggered,How can i Implement this in my cocos2d game.
Is there any method to implement transparent button in cocos2d.
Thanks in advance
When you create a CCMenuItemSprite (a button), you assign it a sprite to use for display.
You can then change the appearance of the button by changing the opacity property of the sprite, or by making it not visible at all.
CCSprite *buttonSpr = [CCSprite spriteWithSpriteFrameName:#"spr.png"];
CCMenuItem *button = [CCMenuItemSprite itemFromNormalSprite:buttonSpr selectedSprite:buttonSpr target:self selector:#selector(buttonTapped:)];
//opacity
buttonSpr.opacity = 50;
//invisible
buttonSpr.visible = false;
I'm not entirely sure that I understand the question and more information would be helpful, but I'll answer it the best I can.
Assuming you have a character class I would implement checkTouchesBegan and do something like this:
-(BOOL) checkTouchesBegan: (CGPoint*) location
{
//conver the touch coordinates to fit your system
int converty = location->y-160;
int convertx = location->x-240;
//determine where the touch is in relation to the center of the character
float ydif = (1.0)*(converty - character_y);
float xdif = (1.0)*(convertx - character_x);
//determine the angle of the touch
float degrees = atan2f(xdif, ydif) * 57;
//determine the distance between the character and the touch
float squared = xdif*xdif + ydif*ydif;
//if the touch is above the character and within a certain distance
if(degrees >= 45 && degrees < 135 && sqrt(squared) < 100)
{
doSomething;
return YES;
}
//if the touch is below the character and within a certain distance
else if(degrees < -45 && degrees >= -135 && sqrt(squared) < 100)
{
doSomething;
return YES;
}
//if the touch is to the right of the character and within a certain distance
else if(degrees >= -45 && degrees < 45 && sqrt(squared) < 100)
{
doSomething;
return YES;
}
//if the touch is to the left of the character and within a certain distance
else if(sqrt(squared) < 100)
{
doSomething;
return YES;
}
return NO;
}
Hope this helps some!

is there anyway we can do rotation of image in a pendulum effect in iphone

is there anyway we can do rotation of image in a pendulum effect in iphone using UIView Animation or CoreAnimation
Use CGAffineTransformRotate to rotate your UIImageView e.g.
yourImage.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI * (_angle/ 180.0));
Also, you'll need to set up the anchor point on your image, prior to rotating it, so that you can make it swing from the correct position. e.g.
yourImage.layer.anchorPoint = CGPointMake(0.5,0); // Set anchor to top middle.
Obviously, you'll need to set up a timer to adjust move the image and control the angle. You could do something like the following in a timer. (untested)
_angle = 45; // Set starting angle.
_direction = 1; // Set starting direction.
-(void) movePend
{
if(_direction == 45){
_direction = 1;
} else if(_direction == 180) {
_direction = 0;
}
_angle = (_direction) ? _angle-- : _angle++; // Determine which way to rotate.
yourImage.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI * (_angle/ 180.0));
}

PagingEnabled for multiple pages in UIScrollView

Edit: See the answer below.
I finally give up and come here to ask you for my problem...
I'm using a UIScrollView for a scrolling menus with little icons.
On each page, with paging enabled, there's an icon in the center, and 2 and a half other visible icons on the left and right. I can move from one icon to its neighbour, and that is fine, but the point is that if I do a fast scrolling, it will not move from more than 3 icons, which is the width of the screen.
What I would want is to be able to scroll on more than 3 icons, and that the magnet behaviour is only triggered when it's slowing down.
I've tried to schedule the scroll view to calculate its velocity, and set the pagingEnabled attribute to NO when it's moving fast and YES again when it's slowing down, but as soon as it is set to YES, the view comes back very fast at its original position, as if it was not detecting that I had brought it to a new page. Would anyone know why it does this? And if I have a way to tell the view "ok, now the paging is enabled but look, you're 15 pages later. Just center on the current page, don't come back at the beginning."
Here's my update function (if it can help):
-(void)update:(ccTime)dt
{
float velocity = fabsf((self.previousOffset-self.scrollView.contentOffset.y)/dt);
self.previousOffset = self.scrollView.contentOffset.y;
CCLOG(#"Velocity: %f", velocity);
if(self.scrollView.pagingEnabled)
{
if(velocity > 100)
{
self.scrollView.pagingEnabled = NO;
}
}
else
{
if(velocity < 100)
{
self.scrollView.pagingEnabled = YES;
}
}
}
I finally found a solution, which was pretty obvious, but that I did not see at the beginning, by using setContentOffset on the scrollView.
Here is the new update function:
-(void)update:(ccTime)dt
{
float velocity = 1000;
if(self.previousOffset)
{
velocity = fabsf((self.previousOffset-self.scrollView.contentOffset.y)/dt);
}
self.previousOffset = self.scrollView.contentOffset.y;
if(velocity < 300)
{
CGSize screenSize = [[CCDirector sharedDirector] winSize];
float halfScreen = screenSize.width/2;
CCLayer *panel = (CCLayer *)[self getChildByTag:1];
SQScrollViewMenu *menu = (SQScrollViewMenu *)[panel getChildByTag:1];
SQMissionItem *currentItem = (SQMissionItem *)[menu getChildByTag:currentPage];
float contentOffsetY = [self.scrollView contentOffset].y;
CCLOG(#"Currentpage: %i ; currentoffsetY: %f", currentPage, contentOffsetY);
float distance = contentOffsetY + [currentItem position].x - halfScreen + panel.position.x + menu.position.x + panel.parent.position.x - 60;
CCLOG(#"Which gives a distance of: %f", distance);
[self.scrollView setContentOffset:CGPointMake(0, distance) animated:YES];
self.previousOffset = 0;
[self unschedule:#selector(update:)];
CCLOG(#"Is unscheduled");
}
}
And it is almost working... at least, it is on simulator. But as soon as I try it on my iPhone 4, it does not work anymore. It always go into that update function, but 7 times among 8, it just blocks the scrollView as it is and does not drags it back to the position I give it... but sometimes it does.
Would anyone have an idea? I found similar issues on the net, but none of them could resolve this...