I have been using storyboards since I began using xCode. Today I have been trying out the old way with xib files.
My first view has an accelerometer code that is still active when I am on the second view.
Is there a way to stop the second view controller from using code from the first view?
I am importing the second views header file into the first views implementation file, is that correct? If I remove that import I get errors.
//
// ViewController.m
#import "ViewController.h"
#import "ViewController2.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
-(void)viewWillAppear:(BOOL)animated{
[self startAccel];
//[self view];
}
-(void)viewWillDisappear:(BOOL)animated{
[self stopAccel];
//[self view];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
double const kThreshold = 1.7;
// double const kThreshold = 2.0;
if ( fabsf(acceleration.x) > kThreshold
|| fabsf(acceleration.y) > kThreshold
|| fabsf(acceleration.z) > kThreshold){
int randomNumber = arc4random() % 3 + 1;
NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:#"Sound%02d", randomNumber] ofType:#"wav"]];
AVAudioPlayer * soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
[soundPlayer prepareToPlay];
[soundPlayer play];
}
}
-(void)startAccel{
UIAccelerometer * accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = .25;
}
-(void)stopAccel{
UIAccelerometer * accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = nil;
}
-(IBAction)View2:(id)sender;{
ViewController2 *V2 = [[ViewController2 alloc]
initWithNibName:#"ViewController2"
bundle:nil];
[self.view addSubview:V2.view];
}
#end
The second controller doesn't really use code from the first but, if you called startAccelerometerUpdates while you were displaying your first view, you might want to stop them as you're about to present the second one if it doesn't need them.
Related
For a exercise I created a project to experiment with the accelerometer functions of the iPhone. Right now when I run the app on my device it begins with a blank screen, shake the phone and a image is displayed.
I have to force close the app to clear the image. I was hoping someone could provide a solution that would make the image reset so I could repeat the process as many times as I wanted. (shake phone, display image, clear image) I'm thinking it needs a timer or something, not sure. Here is the source code. Thanks for taking the time to read and help.
// ViewController.m
// AccelTest
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
-(void)viewWillAppear:(BOOL)animated{
[self startAccel];
[self view];
}
-(void)viewWillDisappear:(BOOL)animated{
[self stopAccel];
[self view];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
double const kThreshold = 2.0;
// double const kThreshold = 2.0;
if ( fabsf(acceleration.x) > kThreshold
|| fabsf(acceleration.y) > kThreshold
|| fabsf(acceleration.z) > kThreshold){
[self.view addSubview:[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Icon.png"]]];
}
}
-(void)startAccel{
UIAccelerometer * accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = .25;
}
-(void)stopAccel{
UIAccelerometer * accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = nil;
}
#end
Here is how I would do it (without ARC) to tap the image to make it disappear.
Remove your line:
[self.view addSubview:[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Icon.png"]]];
And add these lines instead:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Icon.png"]];
myImageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(rm:)];
[myImageView addGestureRecognizer:tapgr];
[tapgr release]; tapgr = nil;
[self.view addSubview:myImageView];
[myImageView release]; myImageView = nil;
Then add a method to the View Controller to remove the UIImageView when it is tapped.
-(void)rm:(UITapGestureRecognizer *)tapgr {
[tapgr.view removeFromSuperview];
}
When that Image is tapped once, the rm: method will be called which will remove the Image from self.view
Store a pointer to that image view somewhere and remove it from it's superview when you want to. Either with a timer, or user action, or something like that.
I was creating a project for a learning exercise, using the accelerometer.
I have the basic functionality of the code, now I just need to make it do something. Display a message, play audio, or display an image.
!! EDIT !!
// ACViewController.m
// Accelerometer
//
#import "ACViewController.h"
#interface ACViewController ()
- (void) startAccelerometer;
- (void) stopAccelerometer;
#end
#implementation ACViewController
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{ //!!EDIT!!
double const kThreshold = 2.0;
if ( fabsf(acceleration.x) > kThreshold
|| fabsf(acceleration.y) > kThreshold
|| fabsf(acceleration.z) > kThreshold)
UILabel * theLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 640)];
theLabel.text = #"Hello earth";
[self.theLabel addSubView:theLabel];
//END OF EDIT
}
- (void)startAccelerometer {
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = self;
accelerometer.updateInterval = 0.25;
}
- (void)stopAccelerometer {
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = nil;
}
- (void)viewDidAppear:(BOOL)animated {
[self startAccelerometer];
}
- (void)viewWillDisappear:(BOOL)animated {
[self stopAccelerometer];
}
#######################################
Header File:
//
// ACViewController.h
// Accelerometer
#import <UIKit/UIKit.h>
#interface ACViewController : UIViewController <UIAccelerometerDelegate>
#end
Example for adding an image (and assuming you use ARC):
In the line with the NSLog, type:
[self.view addSubView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"myImage.png"]]];
Example for a label (ARC):
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 640)];
label.text = #"Hello earth";
[self.view addSubView:label];
When not using ARC, release the instances after adding them to the view.
Previous page:
MainController *bookview=[[MainController alloc]init];
bookview.bookString=booksStr;
[self.navigationController pushViewController:bookview animated:YES];
-transform to next view page:-
- (NSInteger) numberOfPagesForPageFlipper:(AFKPageFlipper *)pageFlipper {
return self.view.bounds.size.width > self.view.bounds.size.height ? ceil((float) CGPDFDocumentGetNumberOfPages(pdfDocument) / 2) : CGPDFDocumentGetNumberOfPages(pdfDocument);
}
- (UIView *) viewForPage:(NSInteger) page inFlipper:(AFKPageFlipper *) pageFlipper {
PDFRendererView *result = [[[PDFRendererView alloc] initWithFrame:pageFlipper.bounds] autorelease];
result.pdfDocument = pdfDocument;
result.pageNumber = page;
return result;
}
-(void)viewDidLoad
{
self.title=bookString;
NSLog(#"the bookstring value is %#",bookString);
}
#pragma mark -
#pragma mark View management
- (void) loadView {
[super loadView];
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
flipper = [[[AFKPageFlipper alloc] initWithFrame:self.view.bounds] autorelease];
flipper.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
flipper.dataSource = self;
NSLog(#"loadview loaded successfully %#",bookString);
[self.view addSubview:flipper];
}
#pragma mark -
#pragma mark Initialization and memory management
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
- (id) init {
if ((self = [super init])) {
pdfDocument = CGPDFDocumentCreateWithURL((CFURLRef) [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"AppleScript Language Guide" ofType:#"pdf"]]);
NSLog(#"this is maincontroller");
[self loadView];
}
return self;
}
- (void)dealloc {
CGPDFDocumentRelease(pdfDocument);
[super dealloc];
}
and I'm not able to transfer the string value and
- changing property opaque in transform-only layer, will have no effect
page not transfer to the nextview
Can anyone help please?
MainController *bookview=[[MainController alloc]init];
bookview.bookString=booksStr;
[self presentViewcontroller:bookview animated:YES];
MainController.h
NSString *bookview;
set property;
#property (nonatomic, retain) NSString *bookview;
MainController.m
#synthesize bookview;
-(void)viewDidLoad
{
NSLog(#"%#",bookview);
}
Hi I am a beginner in programming
I have already created a tapping application, displaying the tap count after pressing the result button
I want to add a NSTimer, counting 30 second after the first tap (after the tap button was pressed for the first time).
at the same time, displaying the time count down on a label (UILabel timeLabel)
and after 30 second, the tap count will restart to 0.
Please kindly tell me if I need to post anything other than the following, Thanks!!
Here is my .h file
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
#class Player;
#interface Tapping2ViewController : UIViewController
<AVAudioPlayerDelegate>
{
Player *aPlayer;
IBOutlet UILabel *timerLabel;
IBOutlet UILabel *resultLabel;
AVAudioPlayer *buttonPlayer;
NSTimer *lv1Timer;
NSInteger *counter1;
}
- (IBAction)addTap:(id)sender;
- (IBAction)getResult:(id)sender;
-(void)restartTapCount;
-(void)start;
#property (retain) NSTimer *lv1Timer;
#property (nonatomic, retain) IBOutlet UILabel *timerLabel;
#end
and my .M file
#import "Tapping2ViewController.h"
#import "Player.h"
#implementation Tapping2ViewController
#synthesize lv1Timer;
#synthesize timerLabel;
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)dealloc
{
[resultLabel release];
[lv1Timer release];
[aPlayer release];
[timerLabel release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
aPlayer = [[Player alloc] init];
[super viewDidLoad];
}
- (IBAction)addTap:(id)sender
{
//呢到係設定聲音, 首先要用NSSTRING 去 SET 左條路徑先
NSString *buttonFile = [[NSBundle mainBundle] pathForResource:#"button" ofType:#"wav"];
//之後再條NSSTRING 轉做NSURL (因為AVPLAYER 只認URL)
NSURL *buttonFileURL = [NSURL fileURLWithPath:buttonFile];
NSError *error = nil;
//設定AUDIO PLAYER 要播邊條 聲音 *記得SET DELEGATE 做自已去執行
buttonPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:buttonFileURL error:&error];
[buttonPlayer setDelegate:self];
NSLog(#"Before: %d", aPlayer.tapCount);
aPlayer.tapCount++;
//呼叫播放既METHOD
[buttonPlayer play];
NSLog(#"After: %d", aPlayer.tapCount);
/*
//即時顯示數字
aPlayer.result = aPlayer.tapCount;
NSString *sResult = [NSString stringWithFormat:#"%D", aPlayer.result];
resultLabel.text = sResult;
*/
}
- (IBAction)getResult:(id)sender {
aPlayer.result = aPlayer.tapCount;
NSString *aResult = [NSString stringWithFormat:#"%D", aPlayer.result];
resultLabel.text = aResult;
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{
aPlayer.tapCount = 0;
resultLabel.text = #"0";
}
}
- (void)viewDidUnload
{
[resultLabel release];
resultLabel = nil;
[timerLabel release];
timerLabel = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//下面係PART OF DETECT SHAKE 既METHOD
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated
{
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
//去到呢到都係
#end
Here is how I do it in an app, on start :
count = COUNTDOWN_DURATION;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self selector:#selector(countDown)
userInfo:nil repeats:YES];
this will call a countDown method every second. Do whatever you want in that countDown method but make sure to stop the NSTimer on completion (and of course to decrement counter):
if (count < 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
...
count--;
I have looked at a few guides for CAKeyFrameAnimation and I am failing to see how to trigger them. The only thing I can think of is that I have to use it as a return, but that doesn't make much sense to me.
-H File-
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface ImageSequenceViewController : UIViewController
<UIGestureRecognizerDelegate>{
UISwipeGestureRecognizer *swipeLeftRecognizer;
NSMutableArray *myImages;
IBOutlet UIImageView *imageView;
IBOutlet UIImageView *bView;
IBOutlet UISegmentedControl *segmentedControl;
}
#property (nonatomic, retain) UISwipeGestureRecognizer *swipeLeftRecognizer;
#property (nonatomic, retain) UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UISegmentedControl *segmentedControl;
-(IBAction)takeLeftSwipeRecognitionEnabledFrom:(UISegmentedControl *)aSegmentedControl;
-(IBAction)ButtonPressed1: (id)sender;
#end
-M File-
#import "ImageSequenceViewController.h"
#implementation ImageSequenceViewController
#synthesize swipeLeftRecognizer;
#synthesize imageView;
#synthesize segmentedControl;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
//CUSTOM CODE
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)loadLeft {
//aView = [[UIImageView alloc] initWithFrame:self.view.frame];
CALayer *layer = [CALayer layer];
[layer setFrame:CGRectMake(0.0,
0.0,
[[self view] frame].size.height,
[[self view] frame].size.width)];
myImages = [[NSMutableArray alloc] init];
for(NSUInteger count=0; count<100; count++){
NSString *fileName;
if (count < 10)
{
fileName = [NSString stringWithFormat:#"trailerRotation_000%d", count];
}
else if (10 <= count < 100)
{
fileName = [NSString stringWithFormat:#"trailerRotation_00%d", count];
}
else
{
fileName = [NSString stringWithFormat:#"trailerRotation_0%d", count];
}
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"jpg"];
//UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileName];
//[myImages addObject:image];
[myImages addObject:[UIImage imageWithContentsOfFile:path]];
}
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:#"Contents"];
[anim setDuration:0.10];
[anim setCalculationMode:kCAAnimationDiscrete];
[anim setRepeatCount:1];
[anim setValues:myImages];
[self.view.layer addSublayer:layer];
[layer addAnimation:anim forKey:#"images"];
//aView.animationImages = myImages;
//aView.animationDuration = 10.00;
//aView.animationRepeatCount = 1;
}
-(void)loadRight {
bView = [[UIImageView alloc] initWithFrame:self.view.frame];
myImages = [[NSMutableArray alloc] init];
for(NSUInteger count=99; count>0; count--){
NSString *countString;
if (count < 10)
{
countString = #"000";
countString = [countString stringByAppendingFormat:#"%d", count];
}
else if (10 <= count < 100)
{
countString = #"00";
countString = [countString stringByAppendingFormat:#"%d", count];
}
else if (100 <= count < 1000)
{
countString = #"00";
countString = [countString stringByAppendingFormat:#"%d", count];
}
NSLog(#"%d", count);
NSString *fileName = #"trailerRotation_";
fileName = [fileName stringByAppendingFormat:countString];
fileName = [fileName stringByAppendingFormat:#".jpg"];
[myImages addObject:[UIImage imageNamed:fileName]];
}
bView.animationImages = myImages;
bView.animationDuration = 10.00;
bView.animationRepeatCount = 1;
}
- (void)viewDidLoad {
[super viewDidLoad];
imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
[imageView setImage:[UIImage imageNamed:#"trailerRotation_0000.jpg"]];
//[self.view addSubview:imageView];
[self loadLeft];
[self loadRight];
UIGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
self.swipeLeftRecognizer = (UISwipeGestureRecognizer *) recognizer;
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
if ([segmentedControl selectedSegmentIndex] == 0) {
[self.view addGestureRecognizer:swipeLeftRecognizer];
}
self.swipeLeftRecognizer = (UISwipeGestureRecognizer *) recognizer;
[recognizer release];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.segmentedControl = nil;
self.swipeLeftRecognizer = nil;
self.imageView = nil;
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
[self becomeFirstResponder];
}
-(void)startItLeft {
NSLog(#"Left");
//[aView startAnimating];
//[self.view addSubview:aView];
//[aView release];
[bView release];
}
-(void)startItRight {
NSLog(#"Right");
[bView startAnimating];
[self.view addSubview:bView];
//[aView release];
[bView release];
}
//- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//}
//- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//}
//- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//}
-(IBAction)ButtonPressed1:(id)sender
{
NSLog(#"Button");
//[aView stopAnimating];
[bView stopAnimating];
//[self loadLeft];
[self loadRight];
}
-(IBAction)takeLeftSwipeRecognitionEnabledFrom:(UISegmentedControl *) aSegmentControl {
if ([aSegmentControl selectedSegmentIndex] == 0) {
[self.view addGestureRecognizer:swipeLeftRecognizer];
}
else {
[self.view removeGestureRecognizer:swipeLeftRecognizer];
}
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *) recognizer {
//CGPoint location = [recognizer locationInView:self.view];
//[self showImageWithText:#"swipe" atPoint:location];
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
//[self startItLeft];
}
else {
[self startItRight];
}
}
//CUSTOM CODE
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)dealloc {
[super dealloc];
}
#end
I'm rather new to iOS development so any advice is helpful.
Thanks.
Try:
[myImages addObject:(id)[UIImage imageWithContentsOfFile:path].CGImage];
Since the CAKeyFrameAnimation expects CGImageRef objects.
It looks like you're trying to load up 100 images and cycle them all within the space of 0.1 second. Not only is that pretty ridiculous (that's 1000fps right there, and CoreAnimation is capped at 60fps), but it's likely to take longer than 0.1 seconds for CoreAnimation to copy all the images before it even starts, which means that it will have finished before it started (and therefore no animation will occur).
I'll admit it was a PEBKAC error.
Thanks all for your help.