I have a CALayer with an image and I have that animates when I touch it, it works, it's great. What I want to do now is also have a sound clip that plays when you press the same image. I'm finding this tricky because the CALayer is from the UIView Class called BounceView. I created an instance of BounceView on my MainViewController. I apologize if I didn't use correct terminology. Do I need to place my audio code in my MainViewController and then use delegation to allow the BounceView to trigger a method on the MainViewController? Any help or direction is greatly appreciated.
MainViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#interface MainViewController : UIViewController <AVAudioPlayerDelegate>
#property (nonatomic, retain) IBOutlet UIView *BounceView;
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- (IBAction)playAudio:(id)sender;
#end
MainViewController.m
#import "MainViewController.h"
#import "BounceView.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize BounceView;
#synthesize audioPlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
//Setup the audio player
NSURL *noSoundFileURL=[NSURL fileURLWithPath:
[[NSBundle mainBundle]
pathForResource:#"InTheMood" ofType:#"mp3"]];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:noSoundFileURL error:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)loadView
{
NSLog(#"loadView");
// Create a view
CGRect frame = [[UIScreen mainScreen] bounds];
BounceView *v = [[BounceView alloc] initWithFrame:frame];
// Set it as *the* view of this view controller
[self setView:v];
}
- (IBAction)playAudio:(id)sender {
// self.audioPlayer.delegate=self;
[self.audioPlayer play];
}
#end
BounceView.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import "MainViewController.h"
#interface BounceView : UIView
{
CALayer *myLayer;
}
#end
BounceView.m
I will provide this code if requested but I don't want to overload code. Here I create new layer open (alloc init), give it size, position, create UIImage, get underlying CGImage & put it on the layer then make sublayer of the view's layer.
Again, any help is greatly appreciated.
Since BounceView is an UIView class not a UIViewController, I would let the playing audio to the MainViewController.
Related
I made a customView that implements its own delegate and set it up inside the main viewController. It works perfectly well, it's the standard practice, there's no need to show any code.
For the sake of better organizing the code I created a manager called customManager that contains the customView and the main viewController's view.
Instead of having all the code that it's needed inside the viewController (managing how the customView is setup and also listening to its delegate) I moved all that code to the customManager instead. That's why I made the customManager just to clear up the code that's inside the viewController.
All renders properly but the delegate never fires.
CustomManager.h
#import "CustomView.h"
#interface CustomManager : NSObject <CustomViewDelegate>
#property (weak) UIView *view; // viewController's view
#property (strong) CustomView *customView;
- (void)load;
#end
CustomManager.m
#import "CustomManager.h"
#implementation CustomManager
// Initialization Method
- (void)load {
_customView = [[CustomView alloc] init];
_customView.delegate = self; // !!!
_customView.frame = CGRectMake(0.0, 20.0, _view.frame.size.width, _view.frame.size.height - 20.0);
[_view addSubview:_customView];
}
#pragma mark - CustomViewDelegate
// Delegate Methods
#end
ViewController.h
#import "CustomManager.h"
#interface ViewController : UIViewController
#property (strong) CustomManager *customManager;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_customManager = [[CustomManager alloc] init];
_customManager.view = self.view;
[_customManager load];
// 1. The load method above removes the code needed here for initializing the customView and setting it up correctly.
// 2. The delegate methods are now in the customManager and leave the space below clear.
}
#end
I have created a custom UIView (ProgressView) where I draw a shape imported via StyleKit from PaintCode.
Below are the codes. I have declared instance variable property in my custom UIView and when I try to change property from ViewController, It does not work.
ProgressView.h
#import <UIKit/UIKit.h>
#interface ProogressView : UIView
#property (nonatomic) float daysFraction;
#property (nonatomic) float pagesFraction;
#end
ProgressView.m
#import "ProgressView.h"
#import "StyleKitName.h"
#import "ViewController.h"
#implementation ProgressView
#synthesize daysFraction = _daysFraction;
#synthesize pagesFraction = _pagesFraction;
- (void)drawRect:(CGRect)rect {
// Drawing code
[StyleKitName drawCanvas1WithDaysFraction:self.daysFraction pageFraction:self.pagesFraction];
}
-(void)awakeFromNib {
[super awakeFromNib];
self.pagesFraction = 0;
self.daysFraction = 0;
}
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
*ViewController.m**
#import "ViewController.h"
#import "ButtonAnimation.h"
#import "ProgressView.h"
#import "StyleKitName.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet ButtonAnimation *buttonView;
#property (weak, nonatomic) IBOutlet UIButton *actionButton;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ProgressView *new =[[ ProgressView alloc]init];
new.daysFraction = 0.7f; // here I am setting value to variable in custom view ProgressView but not working.
}
- (IBAction)animateTheButton:(id)sender {
self.buttonView.layer.backgroundColor = [UIColor clearColor].CGColor;
[self.buttonView addErrorAnimation];
}
#end
You need to add this view to UIViewController's view:
[self.view addSubview:progressView];
Later, you must also set a frame. Eg
[progressView setFrame:self.view.bounds];
You may do it in viewDid/WillLayoutSubviews method to change it on rotation / window resize event.
BTW, do not name your view as new, it's horrible. Such name doesn't even tell what kind of variable is it.
Information: I would like to make a little Game (Not Important what kind of game) with a little Game Over Screen.
Because i cant Upload Images, i try to Describe it.
You Play, everything is alright. Then you get Gameover. The Screen should be the Same and an Overlayer is Showing the Score (Transparent / Alpha)
Now my First Question is: Is my Solution good?
I use one UIViewController from the Storyboard
Source:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIButton *btnTest;
}
-(IBAction)btnTestClick;
#end
ViewController.m
#import "ViewController.h"
#import "MeinGottEh.h"
#interface ViewController ()
#end
#implementation ViewController
-(void)btnTestClick
{
self.modalPresentationStyle = UIModalPresentationCurrentContext;
MeinGottEh *sampleView = [[MeinGottEh alloc] init];
sampleView.labelText = #"High: 123";
[sampleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:sampleView animated:YES];
}
#end
Then the Second UIViewController (External File, not in Storyboard)
MeinGottEh.h
#import <UIKit/UIKit.h>
#interface MeinGottEh : UIViewController
{
IBOutlet UIButton *btnClose;
}
-(IBAction)btnCloseClick;
#property(nonatomic) NSString *labelText;
#end
MeinGottEh.m
#import "MeinGottEh.h"
#interface MeinGottEh ()
#end
#implementation MeinGottEh
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
[btnClose setTitle:self.labelText forState:UIControlStateNormal];
}
-(void)btnCloseClick
{
[self dismissModalViewControllerAnimated:YES];
}
#end
Is this Solution to Open a Highscore Box good? If no, please tell me how to do it (I make a Research on Stackoverflow. Thats the Most Parts)
If Yes: How can i make it Transparent?
Thank you very much.
I am developing application for iOS7 in xcode 5. I have a view controller ADMSViewController
The .h file is as follows
#import <UIKit/UIKit.h>
#import "IMTWebView.h"
#interface ADMSViewController : UIViewController <UIWebViewDelegate,IMTWebViewProgressDelegate>
{
NSString *barcode;
NSString *vinNumber;
NSTimer *timer;
}
#property (nonatomic,retain) IBOutlet UIWebView *webView;
#property (nonatomic,retain) IBOutlet UIProgressView *progressView;
#end
The .m file is as follows
#import "ADMSViewController.h"
#import <QuartzCore/QuartzCore.h>
#implementation ADMSViewController
#synthesize webView;
#synthesize progressView;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com/"]]];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)webView:(IMTWebView *)_webView didReceiveResourceNumber:(int)resourceNumber totalResources:(int)totalResources {
[self.progressView setProgress:((float)resourceNumber) / ((float)totalResources)];
if (resourceNumber == totalResources) {
_webView.resourceCount = 0;
_webView.resourceCompletedCount = 0;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.webView = nil;
self.progressView = nil;
}
#end
The IMTWebview.h file is as follows
#import <UIKit/UIKit.h>
#class IMTWebView;
#protocol IMTWebViewProgressDelegate <NSObject>
#required
- (void) webView:(IMTWebView*)webView didReceiveResourceNumber:(int)resourceNumber totalResources:(int)totalResources;
#end
#interface IMTWebView : UIWebView {
}
#property (nonatomic, assign) int resourceCount;
#property (nonatomic, assign) int resourceCompletedCount;
#property (nonatomic, assign) id<IMTWebViewProgressDelegate> progressDelegate;
#end
the IMTWebCiew.m file is as follows
#import "IMTWebView.h"
#interface UIWebView ()
-(id)webView:(id)view identifierForInitialRequest:(id)initialRequest fromDataSource:(id)dataSource;
-(void)webView:(id)view resource:(id)resource didFinishLoadingFromDataSource:(id)dataSource;
-(void)webView:(id)view resource:(id)resource didFailLoadingWithError:(id)error fromDataSource:(id)dataSource;
#end
#implementation IMTWebView
#synthesize progressDelegate;
#synthesize resourceCount;
#synthesize resourceCompletedCount;
-(id)webView:(id)view identifierForInitialRequest:(id)initialRequest fromDataSource:(id)dataSource
{
[super webView:view identifierForInitialRequest:initialRequest fromDataSource:dataSource];
return [NSNumber numberWithInt:resourceCount++];
}
- (void)webView:(id)view resource:(id)resource didFailLoadingWithError:(id)error fromDataSource:(id)dataSource {
[super webView:view resource:resource didFailLoadingWithError:error fromDataSource:dataSource];
resourceCompletedCount++;
if ([self.progressDelegate respondsToSelector:#selector(webView:didReceiveResourceNumber:totalResources:)]) {
[self.progressDelegate webView:self didReceiveResourceNumber:resourceCompletedCount totalResources:resourceCount];
}
}
-(void)webView:(id)view resource:(id)resource didFinishLoadingFromDataSource:(id)dataSource
{
[super webView:view resource:resource didFinishLoadingFromDataSource:dataSource];
resourceCompletedCount++;
if ([self.progressDelegate respondsToSelector:#selector(webView:didReceiveResourceNumber:totalResources:)]) {
[self.progressDelegate webView:self didReceiveResourceNumber:resourceCompletedCount totalResources:resourceCount];
}
}
#end
My issue is that the progress bar is not animating in the webview. Please help me as I am a newbie to ios development. If there is any other method to implement the same, do inform me.
Thank you in advance for your answers.
For this kind of thing, you can only really 'detect' page size by using Content-Length headers, but otherwise you can still present a loading control (like one of the moving zebra crossing type bars which doesn't show a discrete % progress but shows that activity is occurring.. or even just a UIActivityIndicator). You could try a ready-made class like http://allseeing-i.com/ASIHTTPRequest/ASIWebPageRequest too, which CAN show progress. The author states:
ASIWebPageRequests do not currently support progress tracking for an entire request with its external resources. However, progress updates from external resource requests are passed on to your delegate, so with a bit of work it may be possible to implement this yourself.
I have made a simple app to test this and cannot figure it out. I have an iPad storyboard where I have put two container views as shown below. I have a label in one view and a button in another. My button will increment the label, 1 number at a time.
My problem is not passing the value or incrementing, but getting the view to load the new value.
Each container has its own ViewController
Some code below, although very sparse as Ive written a bunch and deleted as it didn't work. Please help with the correct format. I would like to keep this general format, updating global variable within button and it updating the label.
LabelViewController.h
#import <UIKit/UIKit.h>
#interface LabelViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *checkLabel;
-(void)loadLabel;
#end
LabelViewController.m
#import "LabelViewController.h"
#import "ParentViewController.h"
#interface LabelViewController ()
#end
#implementation LabelViewController
#synthesize checkLabel;
-(void)loadLabel{
checkLabel.text = [NSString stringWithFormat:#"%d",value];
[self.view setNeedsDisplay];
}
- (void)viewDidLoad
{
[self loadLabel];
[super viewDidLoad];
}
ButtonViewController.h
#import <UIKit/UIKit.h>
#interface ButtonViewController : UIViewController
- (IBAction)checkButton:(id)sender;
#end
ButtonViewController.m
#import "ButtonViewController.h"
#import "ParentViewController.h"
#import "LabelViewController.h"
#interface ButtonViewController ()
#end
#implementation ButtonViewController
- (IBAction)checkButton:(id)sender {
value++;
NSLog(#"%d",value);
LabelViewController *fnc = [[LabelViewController alloc] init];
[fnc loadLabel];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
Picture at:
http://farm4.staticflickr.com/3822/9469907420_746db25b23_b.jpg
In ButtonViewController, you don't want to be alloc init'ing an instance of LabelViewController -- the one that's on screen already exists. Both child view controllers are instantiated just before the parent controller is. So, what you need to do is get a reference to the LabelViewController that's on screen. You can do that with self.parentViewController.childViewControllers[0] (that 0 might have to be 1 -- I don't know which controller is which).
LabelViewController *fnc = self.parentViewController.childViewControllers[0];
[fnc loadLabel];