Monday, April 16, 2012

Flip, Grow, and Translate Animation

Look at this video of the MLB At Bat app. Basically, I just want to present a modalViewController with the UIModalPresentationFormSheet style and have it grow from another view then flip. Like when you tap on a game in the scoreboard on the MLB app. Anyone know how I can accomplish this?



Thanks



EDIT: My main view is pretty much the same setup as the MLB app. I'm using AQGridView and want the animation to occur when a cell in the grid view is tapped.



EDIT 2: I'd also be open to ditching the UIViewController concept and just using a plain UIView, then replicate the style of UIModalPresentationFormSheet manually if that's easier.



EDIT 3: Okay, forget using a UIViewController to do this, since I haven't gotten any responses, I'll assume it isn't possible. My new question is just how do I replicate the animation in the posted video using just UIView's? So basically, the initial view needs to grow, move, and flip all at the same time.



EDIT 4: I think I have the actual animation figured out, now my only problem is calculating coordinates to feed into CATransform3DTranslate. My view needs to animate pretty much exactly like in the video. It needs to start over another view and animate to the center of the screen. Here's how I'm trying to calculate the coordinates for the view that pops up in the center:



CGPoint detailInitialPoint = [gridView convertPoint:view.frame.origin toView:detailView.frame.origin];
CGPoint detailFinalPoint = detailView.frame.origin;


gridView is the container view of my main view that holds the smaller grid items. view is the specific grid item that we are animating from. And detailView is the view that comes up in the middle of the screen.





Draw clickable rectangles and I do not know how set params before onDraw method

I am trying to make clickable rectangles. I've looked around and I created quite good code :)



I create constructor of my own rectangle class, then i set some values of it.
However, onDraw method looks like creates and draw rectangle but without constructor's new variables' values.
What do I do wrong?



This is default MyActivity class:



ll = (LinearLayout)findViewById(R.id.linearlayout);
List<MiniRectangle> miniRectangleList = new ArrayList<MiniRectangle>();

for(int i=0;i<8;i++)
{
int numberRandom = r.nextInt(3);

MiniRectangle miniRectangle = new MiniRectangle(this);
miniRectangle.set_color(colors.get(numberRandom));
miniRectangle.set_size(50);
miniRectangle.set_id_color(numberRandom);
miniRectangle.set_number(i);

ll.addView(miniRectangle);

miniRectangleList.add(miniRectangle);
}

setContentView(ll);


This is my own rectangle class



public class MiniRectangle extends View implements View.OnClickListener {

Context context;
int _size;
int _color;
int _id_color;
int _number;

public MiniRectangle(Context context) {
super(context);
this.context = context;
setOnClickListener(this);
}

@Override
public void onClick(View view) {
System.out.println(get_number());
Toast.makeText(context, get_number(), Toast.LENGTH_SHORT).show();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

Paint paint = new Paint();
paint.setColor(get_color());
paint.setStrokeWidth(1);

int kleft = (get_number() % 8) * get_size();
int kright = kleft + get_size() - 2;

int ktop = 1 * get_size();
int kbottom = ktop + get_size() - 2;

canvas.drawRect(kleft, ktop, kright, kbottom, paint);
}
}


I can say that after run project I get only one rectangle. No wonder if every rectangle has the same position. Please Help.





Accessing self from outside of a class

I'm attempting to implement a decorator on certain methods in a class so that if the value has NOT been calculated yet, the method will calculate the value, otherwise it will just return the precomputed value, which is stored in an instance defaultdict. I can't seem to figure out how to access the instance defaultdict from inside of a decorator declared outside of the class. Any ideas on how to implement this?



Here are the imports (for a working example):



from collections import defaultdict
from math import sqrt


Here is my decorator:



class CalcOrPass:
def __init__(self, func):
self.f = func

#if the value is already in the instance dict from SimpleData,
#don't recalculate the values, instead return the value from the dict
def __call__(self, *args, **kwargs):

# can't figure out how to access/pass dict_from_SimpleData to here :(
res = dict_from_SimpleData[self.f.__name__]
if not res:
res = self.f(*args, **kwargs)
dict_from_SimpleData[self.f__name__] = res
return res


And here's the SimpleData class with decorated methods:



class SimpleData:
def __init__(self, data):
self.data = data
self.stats = defaultdict() #here's the dict I'm trying to access

@CalcOrPass
def mean(self):
return sum(self.data)/float(len(self.data))

@CalcOrPass
def se(self):
return [i - self.mean() for i in self.data]

@CalcOrPass
def variance(self):
return sum(i**2 for i in self.se()) / float(len(self.data) - 1)

@CalcOrPass
def stdev(self):
return sqrt(self.variance())


So far, I've tried declaring the decorator inside of SimpleData, trying to pass multiple arguments with the decorator(apparently you can't do this), and spinning around in my swivel chair while trying to toss paper airplanes into my scorpion tank. Any help would be appreciated!





In a hash, how do you add two values for the same key instead of overwriting?

Basically I have these files (medline from NCBI). Each is associated with a journal title. Each has 0, 1 or more genbank identification numbers (GBIDs). I can associate the number of GBIDs per file with each journal name. My problem is that I may have more than one file associated with the same journal, and I don't know how to add the number of GBIDs per file into a total number of GBIDs per journal.



My current code:
jt stands for journal title, pulled out properly from the file. GBIDs are added to the count as encountered.



... up to this point, the first search is performed, each "pmid" you can think of
as a single file, so each "fetch" goes through all the files one at a time...



  pmid_list.each do |pmid|

ncbi_fetch.pubmed(pmid, "medline").each do |pmid_line|

if pmid_line =~ /JT.+- (.+)\n/
jt = $1
jt_count = 0
jt_hash[jt] = jt_count

ncbi_fetch.pubmed(pmid, "medline").each do |pmid_line_2|

if pmid_line_2 =~ /SI.+- GENBANK\/(.+)\n/
gbid = $1
jt_count += 1
gbid_hash["#{gbid}\n"] = nil
end
end

if jt_count > 0
puts "#{jt} = #{jt_count}"

end
end
end
end


My result:



 Your search returned 192 results.
Virology journal = 8
Archives of virology = 9
Virus research = 1
Archives of virology = 6
Virology = 1


Basically, how do I get it to say Archives of virology = 15, but for any journal title? I tried a hash, but the second archives of virology just overwrote the first... is there a way to make two keys add their values in a hash?



Full code:



 #!/usr/local/bin/ruby

require 'rubygems'
require 'bio'


Bio::NCBI.default_email = 'kepresto@uvm.edu'

ncbi_search = Bio::NCBI::REST::ESearch.new
ncbi_fetch = Bio::NCBI::REST::EFetch.new


print "\nQuery?\s"

query_phrase = gets.chomp

"\nYou said \"#{query_phrase}\". Searching, please wait..."

pmid_list = ncbi_search.search("pubmed", "#{query_phrase}", 0)

puts "\nYour search returned #{pmid_list.count} results."

if pmid_list.count > 200
puts "\nToo big."
exit
end

gbid_hash = Hash.new
jt_hash = Hash.new(0)


pmid_list.each do |pmid|

ncbi_fetch.pubmed(pmid, "medline").each do |pmid_line|

if pmid_line =~ /JT.+- (.+)\n/
jt = $1
jt_count = 0
jt_hash[jt] = jt_count

ncbi_fetch.pubmed(pmid, "medline").each do |pmid_line_2|

if pmid_line_2 =~ /SI.+- GENBANK\/(.+)\n/
gbid = $1
jt_count += 1
gbid_hash["#{gbid}\n"] = nil
end
end

if jt_count > 0
puts "#{jt} = #{jt_count}"

end
jt_hash[jt] += jt_count
end
end
end


jt_hash.each do |key,value|
# if value > 0
puts "Journal: #{key} has #{value} entries associtated with it. "
# end
end

# gbid_file = File.open("temp_*.txt","r").each do |gbid_count|
# puts gbid_count
# end




Return "True" on Empty jQuery Selector Array?

I'm working on creating a more semantic way of checking for elements with jQuery. Using $("#element").length > 0 just doesn't really feel very well worded to me, so I'm making my own selector addition for use in .is:



if($("#element").is(":present")) {
console.log("It's aliiiveeee!!");
}


That part was easy, like this:



$.extend($.expr[':'],{
present: function(a) {
return $(a).length > 0;
}
});


I want to go a step further, and make it easy to see if an element doesn't exist, using similar syntax:



$.extend($.expr[':'],{
present: function(a) {
return $(a).length > 0;
},
absent: function(a) {
return $(a).length === 0;
}
});

$(function() {
if($("#element").is(":absent")) {
console.log("He's dead, Jim.");
}
});


But this part is surprisingly hard to do. I think it's because I'm paring down the returned elements to get a result, and paring the selector to .length === 0 is the same as asking for no elelements: it returns false no matter what.



I've tried a lot of different ways to reverse things and get this to return true when the element doesn't exist, and false if it does:



return $(a).length === 0;

return !($(a).length > 0);

if(!($(a).length > 0)) {
return true;
}

if($(a).length > 0) {
return false;
} else {
return true;
}

return !!($(a).length === 0);

// etc...


Is there an easy way to get this to just return true if the element doesn't exist, and false if it does?





How can I get LINQ to accept my argument?

I want to make "Create" option in ASP.NET MVC 3 in C#.
I am working with entity framework.



I want to make a method in one of my models that could insert all the data I posted into my action.



my model looks like this :



public class person { 
public int id { get; set; }
public string name{ get; set; }
public string carBrand { get; set; }
public string streetname{ get; set; }
}


My view is strongly typed with this class above, making a "Create" page. So it creates the textboxes automatically for me.



The object Person, I gave my Controller has a parameter.



public ActionResult Index(person objectPerson)
{
//invoke method here
}


Then I wanted my method, which came from the model, to take this object and add its properties to my database... but the problem is, it doesn't work the way I thought it would.



public void createPerson(person objectPerson)
{
db.MyTable.AddObject(objectPerson)
}


giving the following error : the best overloaded method match has some invalid arguments





Is there a tool where you give a list of things and it returns related words?

Is there some kind of tool you can enter simple queries and it will use AI to process it and provide you an answer relating your query to it's possible meanings? For example:



relate: animal, fast, not bird -> cheetahs, lions, etc
relate: python, else if -> elif, condition blocks, programming
relate: avogadro, constant -> 6.02E23, chemistry, atom, etc
relate: current time, Javascript -> Date.now(), (new Date()).getTime(), etc
relate: [1,5,4,6,2], [1,2,4,5,6] -> sorting algorithm, bubblesort, quicksort
relate: [2,3,6,7], [5,10,37,43] -> x²+1 function
relate: cup, glass, break, sound -> ressonance frequency, waves
relate: wii, controller, movements, electronic component -> accelerometer
relate: cell, division -> mitosis, meiosis, dna, histology, etc
relate: pokemon, red, not game, not version -> charmander, charizard, moltres, etc


Possibly showing relation rates in %, using similar methods to those used on Watson?