Objective-c floating button, same procedure different behaviour - objective-c

I have a created a floating button on a UITableViewController. It works like expected.
Now I wanted to use the button as well on a UIViewController where I added a UITableView.
The code for creating the floating button is identically, but the behaviour is different.
At the UITableViewController the button stay where it shall.
At the UIViewController the button moves up (when I scroll down) and down (when I scroll up)
How do I get the button stay where it shall on the UIViewController. Like on the UITableViewController?
TableViewController.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "SingletonClass.h"
#interface MagTableViewController : UITableViewController <UIGestureRecognizerDelegate>
#property (strong, nonatomic) IBOutlet UITableView *magTableView;
#property (strong, nonatomic) UIButton *oButton;
#property (nonatomic) float originalOrigin;
#end
TableViewController.m - floating button, works like expected
- (void)viewDidLoad
{
[super viewDidLoad];
SingletonClass *sharedSingleton = [SingletonClass sharedInstance];
self.originalOrigin = sharedSingleton.photoButtonY; // 430.0
[self addOverlayButton];
}
#pragma mark Camera Button
- (void)addOverlayButton {
self.oButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.oButton.frame = CGRectMake(132.0, self.originalOrigin, 56.0, 56.0);
[self.oButton setImage:[UIImage imageNamed:#"CameraButton56x56.png"] forState:UIControlStateNormal];
[self.oButton addTarget:self action:#selector(toCameraView:) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:self.oButton aboveSubview:self.view];
}
// to make the button float over the tableView including tableHeader
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect tableBounds = self.tableView.bounds; // should be _magTableView, but works with _tableView as well. But I do not know where this _tableView comes from...
//CGRect tableBounds = self.magTableView.bounds;
// floating Button;
CGRect floatingButtonFrame = self.oButton.frame;
floatingButtonFrame.origin.y = self.originalOrigin + tableBounds.origin.y;
self.oButton.frame = floatingButtonFrame;
[self.view bringSubviewToFront:self.oButton]; // float over the tableHeader
}
ViewController.h
#import <UIKit/UIKit.h>
#import "SingletonClass.h"
#interface DetailViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) UIButton *oButton;
#property (nonatomic) float originalOrigin;
#end
ViewController.m - Jumping Button, not wanted behaviour (Update: works no, no jumping anymore)
- (void)viewDidLoad
{
[super viewDidLoad];
self.originalOrigin = [[SingletonClass sharedInstance] photoButtonY]; // 430.0
[self addOverlayButton];
[self initTableView]; // initiate the tableview and works properly
}
#pragma mark Camera Button
- (void)addOverlayButton {
self.oButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.oButton.frame = CGRectMake(132.0, self.originalOrigin, 56.0, 56.0);
[self.oButton setImage:[UIImage imageNamed:#"CameraButton56x56.png"] forState:UIControlStateNormal];
[self.oButton addTarget:self action:#selector(toCameraView:) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:self.oButton aboveSubview:self.view];
}
/** Remove this function and the button stayed where I expected it
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect tableBounds = self.tableView.bounds;
// floating Button;
CGRect floatingButtonFrame = self.oButton.frame;
floatingButtonFrame.origin.y = self.originalOrigin + tableBounds.origin.y;
self.oButton.frame = floatingButtonFrame;
[self.view bringSubviewToFront:self.oButton]; // float over the tableHeader
}
*/

Related

DrawRect Square/Rectangle user values

I've got the books, I've checked the apple docs and something is not clicking.
Here's what I want to do: get user values and draw a square/rectangle with them.
A window with one custom view on the right.
On the left two text fields, two labels (width and height) and a button "Draw".
On the right DrawView.
There's how one could do it:
You have to do the connection for the textFields and the button in your appDelegate and also have an IBOutlet for your customView.
In your customView you need to have the two variables which will hold the values from the textField. Don't use property/synthesize because once you set the values you need to call setNeedsDisplay in your customView-DrawView.
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import "DrawView.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet DrawView *myDrawView;
#property (weak) IBOutlet NSTextField *widthTextField;
#property (weak) IBOutlet NSTextField *heightTextField;
- (IBAction)draw:(id)sender;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "DrawView.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (IBAction)draw:(id)sender {
[_myDrawView setWidth:[_widthTextField floatValue]];
[_myDrawView setHeight:[_heightTextField floatValue]];
}
DrawView.h
#import <Cocoa/Cocoa.h>
#interface DrawView : NSView {
float width;
float height;
}
-(void) setWidth: (float) aWidth;
-(void) setHeight: (float) aHeight;
#end
DrawView.m
#import "DrawView.h"
#implementation DrawView
#pragma mark - Setters
-(void) setWidht:(float)aWidth {
width = aWidth;
[self setNeedsDisplay:YES];
}
-(void) setHeight:(float)aHeight {
height = aHeight;
[self setNeedsDisplay:YES];
}
#pragma mark - Drawing
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setWidth:10];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
//BackGround Color
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
NSBezierPath* square = [NSBezierPath bezierPath];
[square setLineWidth:2];
NSRect squareRect = NSMakeRect(10,10,width,height);
[square appendBezierPathWithRoundedRect:squareRect xRadius:5 yRadius:5];
[square stroke];
}
#end
That's it. I don't know if there's a better way to do it. It seems a bit bizarre.
Yes it´s nill in drawRect but just before it reaches drawRect it's not because I can calculate the area with the same values...Could you please try it. It takes only 5' to do it.

how do I push a view controller from a ViewCell in a UICollectionView

I'm trying to use a UICollectionView and it's viewCell. I have that going fine. But on the viewcell form I have a button that I intend to have go to a settings view. I'm trying to "push it onto the stack". Xcode complains of "no visible interface for the selector". Some code and the error message are below.
// TryColViewCell.h
#import <UIKit/UIKit.h>
#import "TryColViewContViewController.h"
#interface TryColViewCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UILabel *outletNameLBL;
#property (weak, nonatomic) IBOutlet UILabel *outletCellLabel;
#property (readwrite) NSInteger intTest;
#property (readwrite) TryColViewContViewController *theHost;
- (IBAction)actionPlus1:(id)sender;
- (IBAction)actionMinus1:(id)sender;
- (IBAction)actionNameEdit:(id)sender;
// TryColViewCell.m
#import "TryColViewCell.h"
#import "SettingsViewController.h"
#implementation TryColViewCell {
#public int intCellNumber; //TODO: get this to track the row/cell #
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (IBAction)actionPlus1:(id)sender {
}
- (IBAction)actionMinus1:(id)sender {
}
- (IBAction)actionNameEdit:(id)sender {
NSLog(#"Name Edit");
SettingsViewController *viewControllerB =
[[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
viewControllerB.propName = _outletNameLBL.text;
[self pushViewController:viewControllerB animated:YES];
[self.theHost pushViewController:viewControllerB animated:YES];
}
#end
// Error message on PushVIewController line
TryColViewCell.m:83:11: No visible #interface for 'TryColViewCell'
declares the selector 'pushViewController:animated:'
Push the new view controller from the view controller that contains the collection view, not the cell. Only a subclass of UIViewController has the method pushViewController:animated. to override.
In your table view controller, do something like this, assuming you are using storyboard:
-(UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString* identifier = #"Cell";
CellSubClass *cell =
(CellSubClass*) [tableView dequeueReusableCellWithIdentifier:identifier];
[cell.upButton addTarget:self selector:#selector(actionPlus1:)
forControlEvent:UIControlEventTouchUpInside];
[cell.downButton addTarget:self selector:#selector(actionMinus1:)
forControlEvent:UIControlEventTouchUpInside];
...
}

Building a Camera Application... UIScrollView will not zoom

I'm building a camera application, and I can't get the zooming on the image to work. I honestly don't know where to go from here, I'm sure it's some dumb mistake that I'm missing though. The app sets the chosenImageView to the workingImage (picture from the camera). When I try to zoom, nothing happens.
ViewController.h
#interface ViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UIScrollViewDelegate>
#property (strong, nonatomic) UIImage *workingImage;
#property (weak, nonatomic) IBOutlet UIImageView *chosenImageView;
#property (weak, nonatomic) IBOutlet UIScrollView *imageScroller;
-(IBAction)cameraButtonPressed;
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imageScroller.minimumZoomScale = 0.01f;
self.imageScroller.maximumZoomScale = 6.0f;
self.imageScroller.contentSize = self.imageScroller.frame.size;
self.imageScroller.scrollEnabled = YES;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.chosenImageView;
}
Here was the problem. You have to control+drag from the UIScrollView to the View Controller to set the delegate.

Adding a UIButton to a UIScrollView stops it from scrolling

I'm very new to programming and objective C so you'll have to go easy on me. I'm sure some of the code in the app is probably unnecessary. I've searched google and StackOverflow for an answer but none of the solutions have worked for me, either because they're not the right solution or I just misunderstand the answers.
The problem is that when I add an image that is very long and I want to scroll ONLY vertically, it initially refuses to scroll until I change a "constraint" called "Vertical Space (-1678) - Scroll View - Image View - Ruler pic.png" to 0 for a reason unknown to me. I don't even understand why it defaults it as -1678.
Anyway, it then works perfectly until I add a UIButton in the storyboard (to which I later want to add a modal "action segue" so that it will go to another view). When I've added UIButton it won't scroll at all in the simulator.
I suppose I must have done something wrong because I've only entered one line of code (declaring it) regarding the button, or maybe I have to put some more code in for it to scroll. I don't know! If you need more information in order to help I'll be happy to provide it. I'm fed up with trying to make it work. Thanks in advance.
ViewController.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#interface ViewController : UIViewController <ADBannerViewDelegate, UIScrollViewDelegate> {
ADBannerView *adView;
BOOL bannerIsVisible;
IBOutlet UIScrollView *scrollView;
IBOutlet UIImageView *imageView;
IBOutlet UIButton *proVersion;
}
#property (nonatomic, assign) BOOL bannerIsVisible;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize bannerIsVisible;
#synthesize scrollView;
#synthesize imageView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Hide status bar:
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// iAd:
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50.0f);
[self.view addSubview:adView];
adView.delegate=self;
self.bannerIsVisible=NO;
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, 50.0f);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -50.0f);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I played around a bit and fiddled around with the example in the answer and I got to this code:
scrollView.contentSize = CGSizeMake(320,2246);
which worked! I spent days working on this and it was just one blasted little line of code. I put it into the viewDidLoad and all of a sudden it was scrolling. I also had to turn of "AutoLayout".
You will need to set the ContentSize of the ScrollView, like this:
[scrollView setContentSize:CGSizeMake(320, 500)];

How do I switch between 2 image using UISegmentcontrol?

I've tried writing this code but I don't think it's correct cause there is an error with the viewDidLoad.
I'm trying to switch between 2 images in the same UIImageView using UISegmentcontol using Storyboard. All the examples online are for switching labels and not using Xcode storyboard.
h.file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIImageView *myImage;
IBOutlet UISegmentedControl *segControl;
}
#property(nonatomic, retain)IBOutlet UIImageView *myImage;
#property (nonatomic, retain)IBOutlet UISegmentedControl *segControl;
- (IBAction)switchButton:(id)sender;
#end
m.file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize myImage;
#synthesize segControl;
- (IBAction)switchButton:(id)sender
{
if (segControl.selectedSegmentIndex == 0)
{
[myImage setImage:[UIImage imageNamed:#"image1.jpg"]];
}
else
if (segControl.selectedSegmentIndex == 1)
{
[myImage setImage:[UIImage imageNamed:#"image2.jpg"]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *myImage = [myImage setImage:[UIImage imageNamed:#"image1.jpg"]];
}
If you want to fill the segments of the segmented control with images, you don't need code. Just use the storyboard.