Hi i am using revmob in my game . I have integrated full screen but i cant call more games screen. I have CCMenuItemImage and on its selector i have called
[RevMobAds openAdLinkWithAppID:#"000000000000000"];
its opening itunes. But i want to call [[RevMobAds session] button]; but on revmob docs it is assign to button and in cocos2d i dont have Button i am using CCMenuItemImage.
Link
THis is how it works. (Official Doc)
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat width = floorf(self.view.frame.size.width*.8);
CGFloat height = 80;
CGFloat offset = floorf((self.view.frame.size.width*.8 - width)/2);
UIButton *button = [[RevMobAds session] button];
button.frame = CGrectMake(offset,offset,height,width);
[self.view addSubview:button];
// Optional title change
[button setTitle:#"More Free Games" forState:UIControlStateNormal];
// Optional color changes
UIImage *background1 = [self imageWithColor:[UIColor grayColor]];
UIImage *background2 = [self imageWithColor:[UIColor lightGrayColor]];
[button setBackgroundImage:background1 forState:UIControlStateNormal];
[button setBackgroundImage:background2 forState:UIControlStateSelected];
// Optional rounded corner changes, require #import <QuartzCore/QuartzCore.h>
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;
}
#end
Show free game button only if it loads add. Got this fix from revmob for iOS 6.1.3
-(id)init
{
...
...
[self addRevmobButtonAds];
return self;
}
-(void)addRevmobButtonAds
{
RevMobAdLink *ad = [[RevMobAds session] adLink];
[ad loadWithSuccessHandler:^(RevMobAdLink *link)
{
[self showFreeGameButton];
} andLoadFailHandler:^(RevMobAdLink *link, NSError *error) {
}];
}
-(void) showFreeGameButton
{
CCSprite *more_1 = [CCSprite spriteWithSpriteFrameName:#"moreGamebtn.png"];
CCSprite *more_2 = [CCSprite spriteWithSpriteFrameName:#"moreGameSelected.png"];
CCMenuItemSprite *moreBtn = [CCMenuItemSprite itemFromNormalSprite:more_1
selectedSprite:more_2
target:self
selector:#selector(moreBtnPress:) ];
moreBtn.position = ccp(mS.width*0.75f, mS.height*0.145f);
moreBtn.scale = 0.0f;
CCMenu *menu = [CCMenu menuWithItems:moreBtn, plyBtn, nil];
menu.position = ccp(0.0f,0.0f);
[self addChild:menu z:2 ];
}
-(void)moreBtnPress:(id)sender
{
[[RevMobAds session] showPopup];
[[SimpleAudioEngine sharedEngine] playEffect:#"step.wav" ];
}
You can add a UIButton to a Cocos2D scene the following way:
CGSize winSize = [[CCDirector sharedDirector] winSize];
CGFloat width = floorf(winSize.width*.8);
CGFloat height = 80;
CGFloat offset = floorf((winSize.width*.8 - width)/2);
UIButton *button = [[RevMobAds session] button];
button.frame = CGrectMake(offset,offset,height,width);
[[[CCDirector sharedDirector] view] addSubview:button];
Just note that it will exist over any and all of your Cocos2D nodes.
Related
I have a UIButton inside a UIView, when the UIButton is selected I would like have a background colour of dark gray...is this possible?
UIView *toolbar = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 40, 160)];
[toolbar setBackgroundColor:[UIColor colorWithWhite: 0.70 alpha:0.5]];
toolbar.layer.cornerRadius = 5;
UIButton *penTool = [UIButton buttonWithType:UIButtonTypeCustom];
penTool.frame = CGRectMake(5, 0, 30, 30);
[penTool setImage:[UIImage imageNamed:#"pen-but" inBundle:currentBundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
[penTool addTarget:self action:#selector(drawButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
penTool.autoresizingMask = UIViewAutoresizingNone;
penTool.exclusiveTouch = YES;
penTool.tag = 1;
[toolbar addSubview:penTool];
What if you can do this?
[button setBackgroundColor:[UIColor greenColor]
forState:UIControlStateSelected];
Let's give a great API for every state, not just selected, just like UIButton default API's ability to change image and title for every state.
BGButton.h
#import <UIKit/UIKit.h>
#interface BGButton : UIButton
- (void)setBackgroundColor:(UIColor *)backgroundColor
forState:(UIControlState)state;
#end
BGButton.m
#import "BGButton.h"
#implementation BGButton {
NSMutableDictionary *_stateBackgroundColor;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_stateBackgroundColor = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)setBackgroundColor:(UIColor *)backgroundColor {
[super setBackgroundColor:backgroundColor];
/*
* If user set button.backgroundColor we will set it to
* normal state's backgroundColor.
* Check if state is on normal state to prevent background being
* changed for incorrect state when updateBackgroundColor method is called.
*/
if (self.state == UIControlStateNormal) {
[self setBackgroundColor:backgroundColor forState:UIControlStateNormal];
}
}
- (void)setBackgroundColor:(UIColor *)backgroundColor
forState:(UIControlState)state {
NSNumber *key = [NSNumber numberWithInt:state];
[_stateBackgroundColor setObject:backgroundColor forKey:key];
}
/*
* state is synthesized from other flags. So we can't use KVO
* to listen for state changes.
*/
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
[self stateDidChange];
}
- (void)setEnabled:(BOOL)enabled {
[super setEnabled:enabled];
[self stateDidChange];
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
[self stateDidChange];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self stateDidChange];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[self stateDidChange];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self stateDidChange];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
[self stateDidChange];
}
- (void)stateDidChange {
[self updateBackgroundColor];
}
- (void)updateBackgroundColor {
NSNumber *key = [NSNumber numberWithInt:self.state];
self.backgroundColor = [_stateBackgroundColor objectForKey:key];
}
#end
You can use it like the default UIButton API
BGButton *button = [[BGButton alloc] init];
[button setBackgroundColor:[UIColor greenColor]
forState:UIControlStateSelected];
[button setBackgroundColor:[UIColor blueColor]
forState:UIControlStateHighlighted];
[button setBackgroundColor:[UIColor orangeColor]
forState:UIControlStateDisabled];
Even if you use
button.backgroundColor = [UIColor redColor];
You will stay safe, it will be translated into
[button setBackgroundColor:[UIColor redColor]
forState:UIControlStateNormal];
if you have this code
[self.view addSubview:toolbar];
then just implement your button selector
-(void)drawButtonTapped:(UIButton*)button{
if (button.selected) {
button.backgroundColor = [UIColor clearColor];
button.selected = NO;
}else{
button.backgroundColor = [UIColor darkGrayColor];
button.selected = YES;
}
}
I don't know why guys would go through such trouble when we have this functionality built-in inside UIButton. All you need to do is use UIButtonTypeSystem That's all.
[UIButton buttonWithType:UIButtonTypeSystem]
Seriously, there's nothing more to it. Have a look at it's behavior in the screenshots.
INTERFACE BUILDER
RUN TIME
Want to change color, set tintColor to color of your choice.
ADVANTAGES
Covers titles with dynamic lengths. Not applied on all button width.
When selected, title color is transparent, your background can be seen through, try to use an image as a background.
Let's hope you have at least iOS7 as deployment target. UIButtonTypeSystem was introduce with revised UI design guidelines of iOS7 and has been there for almost 3 years.
Good Luck!
I used a UIButton subclass here:
#import "CustomButton.h"
#interface CustomButton()
#property (nonatomic,strong) UIColor *originalColor;
#end
#implementation CustomButton
- (void)didMoveToSuperview
{
self.originalColor = self.superview.backgroundColor;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
self.superview.backgroundColor = [UIColor grayColor];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
if ([self hitTest:location withEvent:nil]) {
self.superview.backgroundColor = [UIColor grayColor];
self.originalColor = self.superview.backgroundColor;
}
else
{
self.superview.backgroundColor = self.originalColor;
}
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
self.superview.backgroundColor = self.originalColor;
}
#end
If you subclass your button from this class, It should give you the desired effect. This also gives you the highlight effect. If you wish you can disable it by removing the line on touchesBegan.
Just try to Override the UIButton with the following Method...
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.backgroundColor = [UIColor darkGray];
}
}
I think you'll be want to change the selectedImage for flexibility not the background.
Add another image (e.g. pen-but-selected) in the project. Add this 2nd line for setImage:
[penTool setImage:[UIImage imageNamed:#"pen-but-selected" inBundle:currentBundle compatibleWithTraitCollection:nil] forState:UIControlStateSelected];
- (void)drawButtonTapped:(UIButton *)button {
button.selected = !button.selected;
}
If you still want to change background color. #jamil's answer would work. Make sure the buttonImage has transparent part so you can see the buttonBackground through the buttonImage.
Try using a boolean, or some other reference to the selection state of the button. You could also make use of a 'highlightColour' as well as the 'selectedColour' if you wanted closer control (should probably do this for better UX).
bool buttonIsSelected = false;
UIColor * selectedColour = [UIColor redColor];
UIColor * defaultColour = [UIColor blueColor];
UIButton * button = [UIButton new];
button.frame = CGRectMake(0,0,100,100);
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
[button addTarget:self action:#selector(buttonTouchUp:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:#selector(buttonTouchCancel:) forControlEvents:UIControlEventTouchCancel];
[button addTarget:self action:#selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
-(void)buttonTouchUp:(UIButton *)button {
if (buttonIsSelected){
buttonIsSelected = false;
button.backgroundColor = defaultColour;
} else {
buttonIsSelected = true;
button.backgroundColor = selectedColour;
}
}
-(void)buttonTouchCancel:(UIButton *)button {
button.backgroundColor = defaultColour;
if (buttonIsSelected){
button.backgroundColor = selectedColour;
}
}
-(void)buttonTouchDown:(UIButton *)button {
button.backgroundColor = selectedColour;
}
-(void)OnSelect:(UIButton*)button{
if (button.selected) {
button.backgroundColor = [UIColor blue];
button.selected = NO;
}else{
button.backgroundColor = [UIColor darkGrayColor];
button.selected = YES;
}
}
use the above this will work..
-(void)buttonTapped:(UIButton*)button{
button.selected =! button.selected;
if (button.selected)
button.backgroundColor = [UIColor grayColor];
else
button.backgroundColor = [UIColor clearColor];
}
I have a button with two states (i toggle the button using the "selected" state). I would like to animate the transition between the states when user clicks on the button.
I have a list of images that form the animation - what would be the logic to add the animations to the button?
subclass UIButton, override setSelected method this way:
-(void)setSelected:(BOOL)selected{
self.enabled = NO; // disable btn until animation finished
[self setBackgroundImage:nil forState:UIControlStateNormal];
[self setBackgroundImage:nil forState:UIControlStateSelected];
NSArray *imageNames;
// hardcoded frames sequences is for clarity ofcourse =)
if (selected){
// direct frames sequence
imageNames = #[#"normalBG", #"frame0", #"frame1", #"frame2", #"selectedBG"];
} else {
// reversed frames sequence
imageNames = #[#"selectedBG", #"frame2", #"frame1", #"frame0", #"normalBG"];
}
NSTimeInterval animationDuration = (float)imageNames.count/YOUR_ANIMATION_FPS;
NSMutableArray *imageBuffer = [NSMutableArray arrayWithCapacity:imageNames.count];
NSMutableArray *timeBuffer = [NSMutableArray arrayWithCapacity:imageNames.count];
[imageNames enumerateObjectsUsingBlock:^(NSString *name, NSUInteger idx, BOOL *stop) {
[imageBuffer addObject:(__bridge id)[UIImage imageNamed:name].CGImage];
[timeBuffer addObject:#( (float)(idx)/(imageNames.count-1) )];
}];
NSArray *images = [NSArray arrayWithArray:imageBuffer];
NSArray *times = [NSArray arrayWithArray:timeBuffer];
imageBuffer = nil;
timeBuffer = nil;
CAKeyframeAnimation *framesAnimation = [CAKeyframeAnimation animationWithKeyPath:#"contents"];
framesAnimation.values = images;
framesAnimation.keyTimes = times;
framesAnimation.calculationMode = kCAAnimationLinear;
framesAnimation.removedOnCompletion = YES;
framesAnimation.fillMode = kCAFillModeForwards;
framesAnimation.duration = animationDuration;
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[super setSelected:selected];
if (selected){
[self setBackgroundImage:[UIImage imageNamed:#"selectedBG"] forState:UIControlStateSelected];
[self setBackgroundImage:nil forState:UIControlStateNormal];
} else {
[self setBackgroundImage:nil forState:UIControlStateSelected];
[self setBackgroundImage:[UIImage imageNamed:#"normalBG"] forState:UIControlStateNormal];
}
self.enabled = YES;
}];
[self.layer addAnimation:framesAnimation forKey:#"contents"];
[CATransaction commit];
}
don't forget to set the initial background image, i.e. [self setBackgroundImage:[UIImage imageNamed:#"normalBG"] forState:UIControlStateNormal]; in button's init method
I'm new to ios and Objective-C. Please be a little bit patient with me.
THe functionality in my app, which I'm trying to develop, is a small scrolling toolbar, which contains several buttons, state toggling buttons. Also there is another button above the toolbar which slides in and out the toolbar. The second functionality I achieved successfully, using a view for the whole setup and when the slide button touch up event animates the whole view up and down.
For the scrollview, I wanted to add buttons programmatically, hence I drew an outlet to the class and tried adding a self created button as a subview. However I cannot see the button. Nor can I observe the scrollview sliding.
THe following is the way I'm adding the button:
Also, my total view for the above functionality is very less in size than the total view controller
UIButton *button = [[UIButton alloc] init];
button.titleLabel.text = #"hello";
[_scrollTop setContentSize:self.view.frame.size];
[_scrollTop addSubview:button];
The two images down, I hope, should help you understand the functionality I'm trying to develop. If anybody is aware of existence of similar functionality, please do direct me.
Edit: Code totally is
//
// HCILocationViewController.m
// app2
//
// Created by Ravi Vooda on 13/03/13.
// Copyright (c) 2013 housing.co.in. All rights reserved.
//
#import "HCILocationViewController.h"
#import "HCIAnnotationViewController.h"
#interface HCILocationViewController ()
#property int widMap;
#property BOOL showExploreOptions;
#end
#implementation HCILocationViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_mapLocationView.delegate = self;
MKPointAnnotation *annotation = [[HCIAnnotationViewController alloc]
initwithHouse:[self singleHome]];
[_mapLocationView addAnnotation:annotation];
_widMap = 10000;
_showExploreOptions = NO;
/*
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);//give values whatever you want.
[_scrollTop addSubview:button];
*/
[self addButtonsToScrollView];
}
- (void)addButtonsToScrollView
{
NSInteger buttonCount = 4;
CGRect buttonFrame = CGRectMake(5.0f, 5.0f, self.scrollTop.frame.size.width-10.0f, 40.0f);
for (int index = 0; index <buttonCount; index++) {
UIButton *button = [[UIButton alloc]initWithFrame:buttonFrame];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:index+1];
NSString *title = [NSString stringWithFormat:#"Button %d",index+1];
[button setTitle:title forState:UIControlStateNormal];
[self.scrollTop addSubview:button];
buttonFrame.origin.y+=buttonFrame.size.height+5.0f;
}
CGSize contentSize = self.scrollTop.frame.size;
contentSize.height = buttonFrame.origin.y;
[self.scrollTop setContentSize:contentSize];
}
- (void)buttonPressed:(UIButton *)button
{
NSLog(#"button pressed");
switch (button.tag) {
case 1:
//Do something
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
NSString *identifier = #"currDetailsIdentifier";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapLocationView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
return annotationView;
}
-(void) mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(
annotationPoint.x - (_widMap / 2),
annotationPoint.y - (_widMap/2),
_widMap,
_widMap);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[mapView setVisibleMapRect:zoomRect animated:YES];
}
-(void) setMyHouse:(NSDictionary *)singleHouse {
self.singleHome = singleHouse;
}
- (IBAction)toggleExploreOptionsView:(UIButton *)sender {
sender.selected = !sender.selected;
if (_showExploreOptions){
NSLog(#"Close Options");
[UIView animateWithDuration:0.2 animations:^{
_exploreView.frame = CGRectMake(_exploreView.frame.origin.x, _exploreView.frame.origin.y + _exploreOptionsView.frame.size.height, _exploreView.frame.size.width, _exploreView.frame.size.height + _exploreOptionsView.frame.size.height);
}];
}
else {
NSLog(#"Open Options");
[UIView animateWithDuration:0.2 animations:^{
_exploreView.frame = CGRectMake(_exploreView.frame.origin.x, _exploreView.frame.origin.y - _exploreOptionsView.frame.size.height, _exploreView.frame.size.width, _exploreView.frame.size.height - _exploreOptionsView.frame.size.height);
}];
}
_showExploreOptions = !_showExploreOptions;
}
#end
If I understood your question correctly you need to have view which should show up and collapse on a button click event. When the view is shown, it should have a scrollable list of buttons. And you are asking help to show many buttons to scrollView.
Demo Source Code
- (void)addButtonsToScrollView
{
NSInteger buttonCount = 5;
CGRect buttonFrame = CGRectMake(5.0f, 5.0f, self.scrollTop.frame.size.width-10.0f, 40.0f);
for (int index = 0; index <buttonCount; index++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:buttonFrame];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:index+1];
NSString *title = [NSString stringWithFormat:#"Button %d",index+1];
[button setTitle:title forState:UIControlStateNormal];
[self.scrollTop addSubview:button];
buttonFrame.origin.y+=buttonFrame.size.height+5.0f;
}
CGSize contentSize = self.scrollTop.frame.size;
contentSize.height = buttonFrame.origin.y;
[self.scrollTop setContentSize:contentSize];
}
- (void)buttonPressed:(UIButton *)button
{
switch (button.tag) {
case 1:
//Do something
break;
default:
break;
}
}
i am using MPMovieplayerController for playing video, while im clicking some other button i.e i want to pause my video wherever its playing again. i click play means i want to play while its pausing.But for me now when i click button means my video was stopped.But i want to pause instead of stop.
My sample code here
- (IBAction) playvideo
{
NSURL *url = [NSURL URLWithString:#"http://xyz/video1.mp4"];
movieplayer = [[[MPMoviePlayerController alloc]initWithContentURL:url] retain];
movieplayer.view.frame=CGRectMake(25,54,658,460);
[self.view addSubview:movieplayer.view];
[movieplayer play];
}
-(void)buttononclick
{
[movieplayer pause];
[movieplayer.view removeFromSuperview];
for (int i = 0; i < 13; i++)
{
CGRect frame;
frame.origin.x = 150 * i;
frame.origin.y = 0;
frame.size = CGSizeMake(140, self.scrollView.frame.size.height);
[scrollView setShowsHorizontalScrollIndicator:NO];
UIImageView *temp1 = [[UIImageView alloc] initWithFrame:CGRectMake(25, 7, 75, 75)];
[temp1 setImage:[UIImage imageNamed:#"sti15.png"]];
[self.scrollView addSubview:temp1];
UIImageView *temp2 = [[UIImageView alloc] initWithFrame:CGRectMake(110, 7, 75, 75)];
[temp2 setImage:[UIImage imageNamed:#"sti16.png"]];
[self.scrollView addSubview:temp2];
UIImageView *temp3 = [[UIImageView alloc] initWithFrame:CGRectMake(195, 7, 75, 75)];
[temp3 setImage:[UIImage imageNamed:#"sti17.png"]];
[self.scrollView addSubview:temp3];
}
self.scrollView.contentSize = CGSizeMake(165 * 10, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled=0;
}
- (void)viewDidDisappear:(BOOL)animated
{
// [self setDescText:nil];
[super viewDidDisappear:animated];
[movieplayer pause];
[movieplayer.view removeFromSuperview];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ((movie.playbackState==MPMoviePlaybackStateStopped)||(movie.playbackState==MPMoviePlaybackStatePaused))
{
[movie play];
}
else
{
[movie pause];
}
}
Try this Code , it will Work
Check out the notifications for MPMoviePlaybackStatePlaying, MPMoviePlaybackStateStopped, MPMoviePlaybackStatePaused, and MPMoviePlaybackStateInterrupted.
Something like:
MPMoviePlayerController *player = notification.object;
/* Playback is currently stopped. */
if (player.playbackState == MPMoviePlaybackStateStopped)
{
NSLog(#"MPMoviePlaybackStateStopped");
}
/* Playback is currently under way. */
else if (player.playbackState == MPMoviePlaybackStatePlaying)
{
NSLog(#"MPMoviePlaybackStatePlaying");
}
/* Playback is currently paused. */
else if (player.playbackState == MPMoviePlaybackStatePaused)
{
NSLog(#"MPMoviePlaybackStatePaused");
}
You can wire up your target action something like this:
if ((_moviePlayer.playbackState == MPMoviePlaybackStateStopped) || (_moviePlayer.playbackState == MPMoviePlaybackStatePaused)) {
[_moviePlayer play];
} else {
[_moviePlayer pause];
}
you can add target to the play/pause button.
but first you need to catch the button of the mpmovieplayerview.
step 1 list the button. method reference from here
call this method from when the button appear(the video is ready).
But remember, this will also catch the full screen button and airplay button.(if available)
- (void)CatchSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
for (UIView *subview in subviews) {
// Do what you want to do with the subview
NSLog(#"%#", subview);
if(subview isKindOfClass:[UIButton class]]){
// add your target here
[subview addTarget:self action:#selector(extraAction:) forControlEvents:UIControlEventTouchUpInside];
}
// List the subviews of subview
[self listSubviewsOfView:subview];
}
}
step 2 implement the action
-(IBAction)extraAction:(id)sender{
NSLog(#"some extraAction");
}
call the catch method sample.
[self CatchSubviewOfView:movieplayer.view];
I have a lot of trouble to accomplish this task on an ipad : when double tape on an image switch this image to full screen and when double taping again come back to the original display, same thing using pinching. I'm using UIGestureRecognizer to try to do this. Thanks for your help.
GesturesViewController.h
#import <UIKit/UIKit.h>
#interface GesturesViewController : UIViewController
<UIActionSheetDelegate>{
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) UIImageView *imageView;
#end
GesturesViewController.m
#import "GesturesViewController.h"
#import "GesturesAppDelegate.h"
#implementation GesturesViewController
#synthesize imageView;
CGRect originalFrame,fullScreenFrame;
BOOL isFullScreenMode;
- (void)viewDidLoad {
// Loading test image
imageView.image = [UIImage imageNamed:#"image1.jpg"];
//---tap gesture---
isFullScreenMode = NO;
originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);
//changes
fullScreenFrame = CGRectMake(0,0,768,1004);
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];
//---pinch gesture---
UIPinchGestureRecognizer *pinchGesture =
[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinchGesture:)];
[imageView addGestureRecognizer:pinchGesture];
[pinchGesture release];
[super viewDidLoad];
}
//---handle tap gesture---
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
// HOW TO ACCOMPLISH THIS PART
if (isFullScreenMode)
[imageView setFrame:originalFrame];
else
[imageView setFrame:fullScreenFrame];
[imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
isFullScreenMode = !isFullScreenMode;
NSLog(#"Image View : %#",imageView);
}
//---handle pinch gesture---
-(IBAction) handlePinchGesture:(UIGestureRecognizer *) sender {
CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];
if (sender.state == UIGestureRecognizerStateEnded){
// HOW TO ACCOMPLISH THIS ---
if (factor > 1 && !isFullScreenMode) {
//---pinching in---
[imageView setFrame:fullScreenFrame];
} else {
//---pinching out---
[imageView setFrame:originalFrame];
}
isFullScreenMode = !isFullScreenMode;
[imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
}
NSLog(#"Image View : %#",imageView);
}
- (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)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[images release];
[imageView release];
[super dealloc];
}
#end
Thanks.
- (void)toggleZoom:(UITapGestureRecognizer *)gestureRecognizer
{
if (proxyView)
{
CGRect frame =
[proxyView.superview
convertRect:self.view.frame
fromView:self.view.window];
self.view.frame = frame;
CGRect proxyViewFrame = proxyView.frame;
[proxyView.superview addSubview:self.view];
[proxyView removeFromSuperview];
[proxyView autorelease];
proxyView = nil;
self.view.frame = proxyViewFrame;
}
else
{
proxyView = [[UIView alloc] initWithFrame:self.view.frame];
proxyView.hidden = YES;
proxyView.autoresizingMask = self.view.autoresizingMask;
[self.view.superview addSubview:proxyView];
CGRect frame =
[self.view.window
convertRect:self.view.frame
fromView:proxyView.superview];
[self.view.window addSubview:self.view];
self.view.frame = frame;
self.view.frame = self.view.window.bounds;
}
}
I have selected only necessary portion of the code...... its from ZoomingViewController....
If you see its same as we have discussed earlier..... but with few improvements.......
For doing this you have to first store your original frame size somewhere globally so that you can reassing it later on.
you need create two global frames
CGRect originalFrame, fullScreenFrame;
//in viewDidLoad initialize these frames... originalFrame with imageView frame and
fullScreenFrame with the iPad window coordinates........ but remeber this can distort the
aspect ratio so just calculate the aspect ratio of original image by using its height and
width and accordingly create the full screen frame for the image.......
and just assign these frames in your gesture action.
Thanks,
in
viewDidLoad
originalFrame = imageView.frame;
or
originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);
appDelegate <----- get the instance of your appdelegate object so that we can retrieve the window object.......
UIWindow *tempWindow = [appDelegate window];
fullScreenFrame = CGRectMake(tempWindow .frame.origin.x,tempWindow .frame.origin.y,tempWindow .frame.size.width,tempWindow.frame.size.height);
//**in event just set frame of the imageView-- for knowing the current state-- whether its fullScreen or original frame we need to have a flag........it should be global...
so declare a global flag ....BOOL isFullScreenMode and initialize it as NO in
viewDidLoad
isFullScreenMode = NO;
in gesture actions just check this flag and write following...
if (isFullScreenMode)
[imageView setFrame:originalFrame];
else
[imageView setFrame:fullScreenFrame];
isFullScreenMode = !isFullScreenMode;
#implementation ImageFullScreen
#synthesize myImage;
#import "GesturesViewController.h"
#import "GesturesAppDelegate.h"
#implementation GesturesViewController
#synthesize imageView;
CGRect originalFrame,fullScreenFrame;
BOOL isFullScreenMode;
- (void)viewDidLoad {
// Loading test image
imageView.image = [UIImage imageNamed:#"image1.jpg"];
//---tap gesture---
isFullScreenMode = NO;
originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);
//changes
fullScreenFrame = CGRectMake(0,0,768,1004);
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];
//---pinch gesture---
UIPinchGestureRecognizer *pinchGesture =
[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinchGesture:)];
[imageView addGestureRecognizer:pinchGesture];
[pinchGesture release];
[super viewDidLoad];
}
//---handle tap gesture---
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
// HOW TO ACCOMPLISH THIS PART
if (isFullScreenMode)
[imageView setFrame:originalFrame];
else
[imageView setFrame:fullScreenFrame];
[imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
isFullScreenMode = !isFullScreenMode;
NSLog(#"Image View : %#",imageView);
}
//---handle pinch gesture---
-(IBAction) handlePinchGesture:(UIGestureRecognizer *) sender {
CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];
if (sender.state == UIGestureRecognizerStateEnded){
// HOW TO ACCOMPLISH THIS ---
if (factor > 1 && !isFullScreenMode) {
//---pinching in---
[imageView setFrame:fullScreenFrame];
} else {
//---pinching out---
[imageView setFrame:originalFrame];
}
isFullScreenMode = !isFullScreenMode;
[imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
}
NSLog(#"Image View : %#",imageView);
}