How to improve the movement of an image within my ios App - ios

Within my ios App I need to move an image but I am encountering some problems. I am able to move the image correctly to the calculated coordinates, but I need to click on the button that initiates the movement twice before it will appear in the right position, os on the first click the image will only show at the initial position without relocating to the new position.
This is my code:
(EDITED)
Viewcontroller.m
#import "ndpViewController.h"
#interface ndpViewController ()
#end
#implementation ndpViewController
#synthesize image;
-(void)viewDidLoad
{
}
-(void)dealloc
{
}
- (IBAction)calculatePush:(id)sender {
image.hidden=NO;
image.center=CGPointMake(376, 927);
}
- (void)viewDidUnload {
}
#end
Viewcontroller.h
#import <UIKit/UIKit.h>
#interface ndpViewController : UIViewController
{
IBOutlet UIImageView *image;
}
#property (weak, nonatomic) IBOutlet UIButton *calculate;
#property (nonatomic,retain) UIImageView *image;
#end
Any idea why I need to click twice before the image will relocate?. I don't need any kind of animation, just a little image (representing a cross) that will show at a specific point on top of a graphic.
Thank you very much and regards.

I dont exactly understand some of the code. But I think I understood your question which is 'How do I do something first click, and then something else on the second click?'
bool isFirstClick = true;
-(IBAction)CLickEvent:(id)sender{
if(isFirstClick){
//enter first click action here
isFirstClick = false;
}else{
//enter second click action here
isFirstClick = true;
}
}

You have not posted enough code to know for sure. But let me try it this way. What happens if zfw <= 26308 ?? It appears nothing. So in that case nothing moves. How does it EVER move when zfw is <= 26308?
NOTE:
Ok... the code as you have NOW posted it will NEVER relocate because you are always setting the position of the image to the same position on every click.

Actually the problem is even more weird than I thought. The image moves at first click if I type text into any text box within the app, but it will only move at the second click if I type in numbers at the any of those text boxes.
Any suggestion????

Related

How to enable full screen Pan back for UINavigationController

From iOS7 apple provide a way to pan back UINavigationController, but it can only be done by swipe form the edge of the screen. How to enable full screen pan back gesture?
I really spend a lot of time trying to solve this problem, at last I found the way. I just ask a question and answer it myself to share the solution here.
Pls refer my blog to get more detail enter link description here
Demo code:
#import "TestNavigationController.h"
#interface _UINavigationInteractiveTransition
#end
#interface UINavigationController(Custom)
#property (strong, nonatomic) _UINavigationInteractiveTransition * _cachedInteractionController;
#end
#implementation TestNavigationController
{
UIPanGestureRecognizer *_p;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"%#", self.interactivePopGestureRecognizer
);
_p = [[UIPanGestureRecognizer alloc]initWithTarget:self._cachedInteractionController action:#selector(handleNavigationTransition:)];
NSLog(#"%#", self._cachedInteractionController);
[self.view addGestureRecognizer:_p];
}
#end
EDIT NOTE:
This exposes Private APIs.

Connecting a UIScrollView to a UIView class

I'm currently creating a "slide-show" of pictures that the user can scroll through. Following a guide, I made it so that the UIScrollView I am using shows the edges of the previous and next pictures as the user scrolls along. This comes with a side-effect of the user not being able to scroll if he touches on one of the edges of the pictures, because these edges are technically not within the border of the UIScrollView itself. In order to compensate for this, I am going to create a UIView in which I will embed the UIScrollView. The UIVew will be extend the entire width of the page so that the user can scroll when touching the edges of the pictures. To do this, I need to connect the UIScrollView to the code of the UIView through an IBOutlet. Usually this is simply accomplished by Ctrl-clicking on the UIScrollView and dragging to the code. However, this does not seem to work for me and I am stumped as to why.
Here is a screenshot of the environment I am dealing with when I try to ctrl-click on the UIScrollView and drag to the code to create an IBOutlet (it simply doesn't give the option to create anything).
Here is a screenshot of what running the simulator produces. If i try to click and drag where my mouse currently is, it doesn't scroll, which is the problem I am trying to correct.
It is because you should link your storyboard view to UIView class. You can choose your ScrollViewController class in custom class settings. I added the sample jpg
what you are looking for is not that 'easy' by just connecting the two.
There are two options:
1) In the future you should use a UICollectionView and implement the paging behaviour yourself, that is the cleanest code I think.
NEVER MIND THE SECOND OPTION - I DIDN'T SEE YOU ACTUALLY ALREADY USED THIS
2) To directly answer your question:
You have to subclass the UIView that contains the UIScrollView like this:
header:
#import <UIKit/UIKit.h>
#interface SCTouchForwardView : UIView
#property (nonatomic, weak) IBOutlet UIView* receiver;
#property (nonatomic, assign) BOOL force;
#end
Implementation:
#import "SCTouchForwardView.h"
#implementation SCTouchForwardView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView* result = [super hitTest:point withEvent:event];
if (![result isKindOfClass:[SCTouchForwardView class]]) {
return result;
}
if (result==self) {
UIView *result = [self.receiver hitTest:[self convertPoint:point toView:self.receiver] withEvent:event];
if (result) {
return result;
}else{
if (self.force) {
return self.receiver;
}
return nil;
}
}
return nil;
}
#end
When you want to use this, you just have to Right-Click-Drag from the container to the UIScrollview and select 'receiver' and what you tried to do will work!

Adding tag after switching view controllers

I have a randomly generated int that I assign to a tag for a UIImageView on my storyboard in ViewWillAppear.
When I segue to the main menu and try to enter the view again, however, the app crashes. I know it's because of the tag because when I remove it everything works fine.
Any thoughts on why it works the first time but not on times after that? Code below.
ViewController.h:
#interface ViewController : UIViewController{
int tagnumber;
IBOutlet UIImageView *box;
...
}
ViewController.m:
-(void)viewWillAppear:(BOOL)animated{
tagnumber = arc4random()%1000;
box.tag = tagnumber;
...
}
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
{
[_animator removeAllBehaviors];
[box removeFromSuperview];
}
MainMenu.m:
-(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue {
}
Basically, when the view disappears and the system is running out of memory, it will call on UIViewController's implementation of didReceiveMemoryWarning. There is a fantastic description of this here: ViewWillDisappear versus dealloc
When you are creating an IBOutput you should really have a weak pointer to it by saying #property (weak, nonatomic) IBOutlet UIImageView *box. Views retain their subviews, and therefore, if a view gets deallocated, its subviews retain count decreases by one. When the view disappears, the system will decide when to deallocate self.view, and when it does, it will automatically release box, and box will get deallocated as well. You don't really need to worry about it.

How to set label text to a variable as the view loads?

So I have this little project in which the user goes from one view to another as they click buttons, and the text on the last button they've clicked should show up in a label in the top of the screen. I figured out how to have the variables accessible from anywhere in the app, and also how to change it as the user presses the buttons. Yet, I can only make the label's text change once a button is hit, and I wanted it to happen automatically (the view to appear with the label already containing the correct text).
For that I set the label in two different ways (which I found online):
#interface ViewMateria : UIViewController
{
IBOutlet UILabel *materiaLabel;
}
#end
and
#interface ViewMateria : UIViewController
#property (weak, nonatomic) IBOutput UILabel *materiaLabel;
#end
Still, for both of this I can not access the label by a simple materiaLabel.text or anything like that, and can't find a way to do it without the need for the press of a button.
I've been looking for an answer for this for a while now, and nothing that I found seemed to be of use for me (since most of the links I found taught how to change a label text with the press of a button).
This is my first project with Objective-C and Xcode, and I don't really know the answer, so maybe I am missing something quite obvious, but still, if anyone could point me in the right direction, that would be of much help.
Put this in your .h file for the view controller:
#property (weak, nonatomic) IBOutput UILabel *materiaLabel;
When you are about to present the view controller, access it like this:
your_ViewMateria_object.materiaLabel.text = #"";
Then present it..
Are you using segues?
If yes, its another story..
UPDATE:
For segues use this, with the .h property value I just mentioned:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ViewMateria *vm = [segue destinationViewController];
vm.materiaLabel.text = #"Whatever";
}
You should be good to go now ;D
Did it work?
UPDATE
In second view controller .h file
#property (strong, nonatomic) NSString *buttonName;
.m file
self.materiaLabel.text = self.buttonName;
In first view controller .m file
inside IBAction
secondviewcontrollerObject.buttonName = [(UIButton *)sender currentTitle];

Disabling all buttons until an IBAction completed

I have four buttons on a single view. Each button has a different IBaction. Once one is pressed I would like to lock out all buttons from being pressed again until the action is completed. I have tried to give each one a property in the .h and then under each IBAction in the .m, I have set each of the four buttons like this btnHelp.enabled =NO;,., etc and then at the end before the final } I have reenabled them all. This hasn't worked. I am a new to objective-C and xcode and greatly appreciate any help.
This is IOS. The .h code is :
#property (strong, nonatomic) IBOutlet UIButton *btnHelp;
#property (strong, nonatomic) IBOutlet UIButton *btnSpin;
#property (strong, nonatomic) IBOutlet UIButton *btnScore;
#property (strong, nonatomic) IBOutlet UIButton *btnBet;
- (IBAction)slotHelp:(id)sender;
- (IBAction)slotSpin:(id)sender;
- (IBAction)slotBet:(id)sender;
- (IBAction)slotScore:(id)sender;
The .m is :
#synthesize btnHelp, btnScore, btnSpin, btnBet;
- (void)viewDidLoad
{
[super viewDidLoad];
btnHelp.enabled = NO;
btnBet.enabled = NO;
btnScore.enabled = NO;
btnSpin.enabled = NO;
// do some introductory setup and give some spoken instructions
// Load plist data into array, etc
// Then reenable buttons
btnHelp.enabled=YES;
btnBet.enabled=YES;
btnScore.enabled=YES;
btnSpin.enabled=YES;
}
-(IBAction)slotHelp {
// Disable buttons while we complete action
btnHelp.enabled = NO;
btnBet.enabled = NO;
btnScore.enabled = NO;
btnSpin.enabled = NO;
// stuff to do
// Reenable buttons after action event completed
btnHelp.enabled=YES;
btnBet.enabled=YES;
btnScore.enabled=YES;
btnSpin.enabled=YES;
}
There are three other action events for the other three buttons.
What I was trying to do is prevent accidental button presses during and action execution. I was hoping to have no chance of simultaneous button presses by accident. I am trying to make an app for use by physically challenged individuals who may accidentally press more than one button. I hope this helps explain a bit of why I want to block multiple presses even the same button.
Thanks for your interest and help.
As one of the most simple solution you may add a bool flag variable to your view class and add a check in you IBaction methods. Something like this
- (void)viewDidLoad
{
[super viewDidLoad];
lock = true;
// do some introductory setup and give some spoken instructions
// Load plist data into array, etc
// Then reenable buttons
lock = false;
}
-(IBAction)slotHelp {
if(!lock) {
lock = true;
//do what you need
lock = false;
}
and do the same check for all the other IBActions
As alternative way, you can globally block-unblock events with methods posted below, instead of blocking each button separately:
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
and
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
But be careful. Wrong strategy of using of these methods can block your application.

Resources