Git Product home page Git Product logo

lpswipeableinputmatrixview's Introduction

MYSwipeableInputMatrixView

This is a example of how to make these swipe-on-a-matrix games. The simplest way to get started is by open up the example Xcode project and look in MYViewController.m

##Setup the project

  • Open Xcode, create new project (iOS: Single View Application).

  • Select MYViewController.xib and delete it (Move to trash)

  • In MYAppDelegate.m, change

      self.viewController = [[MYViewController alloc] initWithNibName:@"MYViewController" bundle:nil];
    

    to

      self.viewController = [[MYViewController alloc] init];
    
  • Download this repo as zip, drag the folder MYSwipeableInputMatrixView into the project (sidebar)

  • Check "Copy items.." and "Create Groups..", hit return.

  • Click your project name in the sidebar

  • Make sure that portrait only is selected for now

MYViewController.m:

  • At top:

      #import "MYSwipeableInputMatrixView.h"
      #import "MYSwipeableBrick.h"
    
  • Before viewDidLoad:

      MYSwipeableInputMatrixView *imvw;
    
  • Inside viewDidLoad:

      self.view = [[UIView alloc] init];
      self.view.backgroundColor = [UIColor whiteColor];
      self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
      
      // For iPhone 4 and 5 portrait
      CGSize size = [[UIScreen mainScreen] bounds].size;
      CGFloat width = MIN(size.width, size.height);
      CGFloat height = MAX(size.width, size.height)-20;
      CGFloat imvwH = 200;
    
  • And now, initiate the matrix! (Still in viewDidLoad)

      imvw = [[MYSwipeableInputMatrixView alloc] initWithRows:4 cols:4];
      imvw.borderWidth = 3;
      imvw.touchBorderWidth = 7; // So you can make diagonal swipes
      imvw.frame = CGRectMake(0, height-imvwH, width, imvwH);
      imvw.backgroundColor = [UIColor clearColor];
      [self.view addSubview:imvw];
    
      // Set titles and brick colors
      for (int i=0; i<imvw.rows*imvw.cols; i++) {
      	
      	NSInteger nr = rndim(10, 1);
      	[imvw.titles replaceObjectAtIndex:i withObject:@(nr).stringValue];
      	MYSwipeableBrick *brick = [imvw.bricks objectAtIndex:i];
      	
      	CGFloat hue = nr/10.0;
      	brick.color					= [UIColor colorWithHue:hue saturation:1.0 brightness:1.0 alpha:1];
      	brick.textNormalColor		= [UIColor colorWithHue:hue saturation:0.5 brightness:1.0 alpha:1];
      	brick.highlightColor		= [UIColor colorWithHue:hue saturation:1.0 brightness:0.6 alpha:1];
      	brick.textHighlightColor	= [UIColor colorWithHue:hue saturation:0.5 brightness:0.6 alpha:1];
      	
      }
    
      [imvw updateTitles];
    
  • Simple and buggy code for making swipes work! (Please improve it for your needs..)

      [imvw setBrickSwipeEventBlock:^(MYSwipeEventStatus status, MYSwipeableInputMatrixView *mvw, CGPoint loc, NSInteger index, NSInteger lastIndex) {
      	if (status==MYSwipeEventStatusBegan) {
      		mvw.selectedBricks = [[NSMutableArray alloc] initWithCapacity:mvw.rows*mvw.cols];
      	}
      	
      	if (status==MYSwipeEventStatusBegan || status==MYSwipeEventStatusChanged) {
      		if (index==-1) return;
      		
      		MYSwipeableBrick *brick = [mvw.bricks objectAtIndex:index];
      		brick.highlighted = YES;
      		
      		[mvw.selectedBricks addObject:@(index)];
      		
      	} else if (status==MYSwipeEventStatusEnded) {
      		
      		for (int i=0; i<mvw.rows*mvw.cols; i++) {
      			MYSwipeableBrick *brick = [mvw.bricks objectAtIndex:i];
      			brick.highlighted = NO;
      		}
      		
      		NSString *msg = @"Select done!";
      		for (int i=0; i<mvw.selectedBricks.count; i++) {
      			MYSwipeableBrick *brick = [mvw.bricks objectAtIndex:[[mvw.selectedBricks objectAtIndex:i] integerValue]];
      			msg = [msg stringByAppendingString:@" "];
      			msg = [msg stringByAppendingString:brick.text];
      		}
       
      		NSLog(@"%@", msg);
      		
      	}
      }];
    
  • Run and enjoy! :)

##More

The project is licenced under the MIT Licence included in this repo. If you plan to make an app of it, i would be glad if you told me about it! The code isn't perfect at all, but could give some insperation. Actually, in the beginning, it was my answer to the StackOverflow question http://stackoverflow.com/questions/17927534/detecting-touchdown-touchup-touchmove-events-across-4x4-ios-tiles, but later it evolved into this project.

Made by Leonard Pauli, 30 july 2013.

lpswipeableinputmatrixview's People

Contributors

leonardpauli avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.