UIViewController pass float value to UIView method - ios

I have a ViewController with a HandlePan method:
-(void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
CGFloat panSpeed = (translation.x+translation.y)/2;
NSLog(#"PanSpeed %f",panSpeed);
[someview viewPan:panSpeed];
}
}
Then I have a UIView called someview with the method:
- (void) viewPan:(float *)PanAngle {
NSLog(#"Panning...%#",PanAngle);
}
But I am getting a nil for PanAngle.
What is the proper way of doing this? Thanks!

float isn't an object, so no need for a pointer
- (void)viewPan:(float)PanAngle{
NSLog(#"Panning...%f",PanAngle);
}

Related

UIPanGestureRecognizer get swipe distance in pixels

I'm trying to track the horizontal distance that a user has swiped using a UIPanGestureRecognizer, but I'm having difficulty interpreting the results. Here is the code that I am using:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint startLocation;
if (recognizer.state == UIGestureRecognizerStateBegan) {
startLocation = [recognizer locationInView:self.view];
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint stopLocation = [recognizer locationInView:self.view];
CGFloat dx = stopLocation.x - startLocation.x;
NSLog(#"dx: %f", dx);
}
}
If I swipe left-to-right, I get output something like this:
dx: 50328911327402404790403072.000000
My screen is only 320 pixels wide, so my end result cannot be greater than 320. Is there a problem with my code or am I just interpreting this number incorrectly? How can I get this value stated in pixels? Thanks!
startLocation doesn't persist after handlePan: returns, so you're getting garbage.
Either declare startLocation as static or save it in an instance variable / property.
You can use UIPanGestureRecognizer as below:-
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY);
[[sender view] setCenter:translatedPoint];
if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat velocityX = (0.2*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalX = translatedPoint.x + velocityX;
CGFloat finalY = firstY;

Gesture Recognizer Pinch and Pan at same pic - loosing centre for pinching

My problem is that I can't use Pinch and Pan(
both functions work perfectly separate. but if I move pic - zooming have the wrong centre anchor (between 2 fingers)
code:
//fullScreenImage is UIImageView that i would like to manipulate
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scalePiece:)];
pinchRecognizer setDelegate:(id)self];
[fullScreenImage.superview addGestureRecognizer:pinchRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:(id)self];
[self.view addGestureRecognizer:panRecognizer];
- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
[gestureRecognizer setScale:1];
}
//Setting anchor between 2 fingers to zoom correctly
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = CGPointMake([gestureRecognizer locationInView:piece].x,[gestureRecognizer locationInView:piece].y);
CGPoint locationInSuperview = CGPointMake([gestureRecognizer locationInView:piece.superview].x,[gestureRecognizer locationInView:piece.superview].y);
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}
- (void)move:(UIPinchGestureRecognizer *)recognizer
{
if ([(UIPanGestureRecognizer*)recognizer state] == UIGestureRecognizerStateBegan) {
position.y=[recognizer locationInView:recognizer.view].y-fullScreenImage.center.y;
position.x=[recognizer locationInView:recognizer.view].x-fullScreenImage.center.x;
}
fullScreenImage.center=CGPointMake([recognizer locationInView:fullScreenImage.superview].x-position.x, [recognizer locationInView:fullScreenImage.superview].y-position.y);
}
Try this One:
Add this method in your code :
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
And call this method when you are using gesture.
- (void)scalePiece_image:(UIPinchGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged)
{
[gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
[gestureRecognizer setScale:1];
}
}
SR Nayak answer is not correct because the pivot will change when you pinch and the layer will slide.
This code works:
- (IBAction)pinchLayer:(UIPinchGestureRecognizer *)gestureRecognizer {
static CGPoint lastPoint;
static CGFloat lastScale;
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
lastScale = 1.0;
lastPoint = [gestureRecognizer locationInView:self.containerView];
}
// Scale
CGFloat scale = 1.0 - (lastScale - gestureRecognizer.scale);
[[gestureRecognizer view].layer setAffineTransform:
CGAffineTransformScale([[gestureRecognizer view].layer affineTransform],
scale,
scale)];
lastScale = gestureRecognizer.scale;
// Translate
CGPoint point = [gestureRecognizer locationInView:self.containerView];
[[gestureRecognizer view].layer setAffineTransform:
CGAffineTransformTranslate([[gestureRecognizer view].layer affineTransform],
point.x - lastPoint.x,
point.y - lastPoint.y)];
lastPoint = [gestureRecognizer locationInView:self.containerView];
}

Save the position of a UIImageView

I have several UIImageViews that the user can drag, scale and rotate. I want the position, size and angle to persist when the user navigates to another view and comes back. How do i achieve this?
I have used the following code to pan, rotate and scale
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
[piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
[gestureRecognizer setRotation:0];
}
}
- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
[gestureRecognizer setScale:1];
}
}
Depending on your needs you can create method, that you'll add as another target method to all your gesture recognizers.
E.g.
[panGestureRecognizer addTarget: self action: #selector(onGestureRecognizerEnd:)];
[rotationGestureRecognizer addTarget: self action: #selector(onGestureRecognizerEnd:)];
[pinchGestureRecognizer addTarget: self action: #selector(onGestureRecognizerEnd:)];
Sample implementation of that method could be:
- (void)onGestureRecognizerEnd: (UIGestureRecognizer *)gestureRecognizer
{
switch ( gestureRecognizer.state )
{
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateFailed:
{
[self _saveViewPosition: gestureRecognizer.view];
break;
}
default:
{
break;
}
}
}
_saveViewPosition: method you can implement like this:
- (void)_saveViewPosition: (UIView *)view
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: [NSKeyedArchiver archivedDataWithRootObject: [NSValue valueWithCGPoint: view.center]]
forKey: [self _centerKeyForView: view]];
[defaults setObject: [NSKeyedArchiver archivedDataWithRootObject: [NSValue valueWithCGAffineTransform: view.transform]]
forKey: [self _transformKeyForView: view]];
}
For more information check Xcode project.
BR

Rotate the custom view?

I've some drawings on my custom view, want to let users rotate it
viewContorller.m:
-(void)setMyView:(myView *)myView {
...
[self.faceView addGestureRecognizer:[[UIRotationGestureRecognizer alloc] initWithTarget:faceView action:#selector(rotate:)]];
...
}
faceView.m
- (void)rotate:(UIRotationGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateChanged) {
self.transform = CGAffineTransformMakeRotation(gesture.rotation);
gesture.rotation = 0;
}
}
It just not work, but quite shaking?
Use following code and also add UIGestureRecognizerDelegate in viewController.h class.
-(void)setMyView:(myView *)myView {
//...
UIRotationGestureRecognizer* rotateRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
rotateRecognizer.delegate = self;
[self.faceView addGestureRecognizer:rotateRecognizer];
//...
}
I think it will be helpful to you.
Ok, the problem is with your rotate method, try this code,
- (void)rotate:(UIRotationGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateChanged) {
self.transform = CGAffineTransformRotate(self.transform, [gesture rotation]);
gesture.rotation = 0;
}
}
Use this code to rotate your view ,enable userinteraction and multi touch enable on you view.
- (void)viewDidLoad
{
[super viewDidLoad];
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotatePiece:)];
[self.myview addGestureRecognizer:rotationGesture];
}
- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
[gestureRecognizer setRotation:0];
}
}
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}
This code is from apples sample code touches.

Setting position of UIView using IF statement

In the application I am creating, I have a UIView named AdvancedView1. The user can drag the view around the screen. The user needs to drag the view to a certain position on the screen. If the view is dropped into a certain area, then the view would "snap" into a defined location. When trying to use IF AND statements, I am getting an error. Any suggestions?
Also "origin" is a CGPoint declared in the .h file.
- (void)pan:(UIPanGestureRecognizer *) gesture
{
if (gesture.state == UIGestureRecognizerStateChanged)
{
CGPoint origin = [gesture locationInView:[self superview]];
[self bringSubviewToFront:[self superview]];
[self setCenter:origin];
}
if (gesture.state == UIGestureRecognizerStateEnded)
{
if (origin.x >= 574.0 && origin.x <= 724.0 ) {
self.frame = CGRectMake(574.0, 184.0, self.frame.size.width, self.frame.size.height);
}
}
}
I am getting the following error for "origin.x >= ...."
"Request for member "x" in something not a structure or union"
origin variable scope is limited to the 1st if block and it is not visible in the 2nd one. Move its declaration to the function scope:
- (void)pan:(UIPanGestureRecognizer *) gesture
{
CGPoint origin = [gesture locationInView:[self superview]];
if (gesture.state == UIGestureRecognizerStateChanged)
{
[self bringSubviewToFront:[self superview]];
[self setCenter:origin];
}
if (gesture.state == UIGestureRecognizerStateEnded)
{
if (origin.x >= 574.0 && origin.x <= 724.0 ) {
self.frame = CGRectMake(574.0, 184.0, self.frame.size.width, self.frame.size.height);
}
}
}

Resources